PHP Curl和Socket请求方式
function file_get_contents_new($url){ if(function_exists('curl_init')){ $t = new Timer(); $t->start(); $ch = curl_init(); curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_HEADER, 0); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT,2); curl_setopt ($ch, CURLOPT_DNS_USE_GLOBAL_CACHE, 1); curl_setopt ($ch, CURLOPT_HTTPPROXYTUNNEL, 1); $content = curl_exec($ch); if($errno = curl_errno($ch)){ log::write( get_domain($url) .':'. curl_error($ch) . " errno:{$errno}"); } curl_close($ch); $t->stop(); if($t->spent() >= 3){ log::write( "url: {$url}, time:{$t->spent()}" ); } //记录消耗的时间 req_black_server($t->spent(), get_domain($url)); if($content){ return $content; } } return false; } //post数据 function yf_post($url, $data='', $timeout=10){ return httpRequestPOST($url, $data); } function httpRequestPOST($url,$post_data){ $t = new Timer(); $t->start(); $url2 = parse_url($url); $url2["path"] = ($url2["path"] == "" ? "/" : $url2["path"]); $url2["port"] = ($url2["port"] == "" ? 80 : $url2["port"]); $host_ip = @gethostbyname($url2["host"]); if(!$host_ip){ log::write("[{$url2["host"]}] gethostname error"); } $fsock_timeout= 10;//秒 if(($fsock = fsockopen($host_ip, $url2["port"], $errno, $errstr, $fsock_timeout)) < 0){ log::write("[{$url2["host"]}] fsockopen connect errno:{$errno} errstr:{$errstr}"); return false; } $request = $url2["path"] . ($url2["query"] != "" ? "?" . $url2["query"] : "") . ($url2["fragment"] != "" ? "#" . $url2["fragment"] : ""); /* $needChar = false; foreach($post_data as $key => $val) { $post_data2 .= ($needChar ? "&" : "") . urlencode($key) . "=" . urlencode($val); $needChar = true; } */ $post_data2 = $post_data; $in = "POST " . $request . " HTTP/1.0\r\n"; $in .= "Accept: */*\r\n"; $in .= "Host: " . $url2["host"] . "\r\n"; $in .= "User-Agent: Lowell-Agent\r\n"; $in .= "Content-type: application/x-www-form-urlencoded\r\n"; $in .= "Content-Length: " . strlen($post_data2) . "\r\n"; $in .= "Connection: Close\r\n\r\n"; $in .= $post_data2 . "\r\n\r\n"; unset($post_data2); if(!@fwrite($fsock, $in, strlen($in))){ //出错 log::write("[{$url2["host"]}] fwrite error data:{$request} in:{$in}"); fclose($fsock); return false; } unset($in); $out = ""; while($buff = fgets($fsock, 2048)){ $out .= $buff; } fclose($fsock); $pos = strpos($out, "\r\n\r\n"); $head = substr($out, 0, $pos); //http head $status = substr($head, 0, strpos($head, "\r\n")); //http status line $body = substr($out, $pos + 4, strlen($out) - ($pos + 4));//page body $t->stop(); //记录消耗的时间 req_black_server($t->spent(), get_domain($url)); if(preg_match("/^HTTP\/\d\.\d\s([\d]+)\s.*$/", $status, $matches)){ if(intval($matches[1]) / 100 == 2){ return $body; } } log::write("[{$url2["host"]}] body is null"); return false; } function httpRequestGET($url){ $t = new Timer(); $t->start(); $url2 = parse_url($url); $url2["path"] = ($url2["path"] == "" ? "/" : $url2["path"]); $url2["port"] = ($url2["port"] == "" ? 80 : $url2["port"]); $host_ip = @gethostbyname($url2["host"]); //解析域名 if(!$host_ip){ log::write("[{$url2["host"]}] gethostname error"); } $fsock_timeout= 10; if(!$fsock = fsockopen($host_ip, $url2["port"], $errno, $errstr, $fsock_timeout)){ log::write("[{$url2["host"]}] fsockopen connect errno:{$errno} errstr:{$errstr}"); return false; } //log::write("[{$url2["host"]}] fsockopen connect success"); //sock连接成功 $request = $url2["path"] . ($url2["query"] != "" ? "?" . $url2["query"] : "") . ($url2["fragment"] != "" ? "#" . $url2["fragment"] : ""); $in = "GET " . $request . " HTTP/1.0\r\n"; $in .= "Accept: */*\r\n"; $in .= "User-Agent: Payb-Agent\r\n"; $in .= "Host: " . $url2["host"] . "\r\n"; $in .= "Connection: Close\r\n\r\n"; if(!@fwrite($fsock, $in, strlen($in))){ //出错 log::write("[{$url2["host"]}] fwrite error"); fclose($fsock); return false; } unset($in); $out = ""; while($buff = @fgets($fsock, 2048)){ $out .= $buff; } fclose($fsock); $pos = strpos($out, "\r\n\r\n"); $head = substr($out, 0, $pos); //http head $status = substr($head, 0, strpos($head, "\r\n")); //http status line $body = substr($out, $pos + 4, strlen($out) - ($pos + 4));//page body $t->stop(); if($t->spent() >= 3){ log::write( "url: {$url}, time:{$t->spent()}" ); } //记录消耗的时间 req_black_server($t->spent(), get_domain($url)); if(preg_match("/^HTTP\/\d\.\d\s([\d]+)\s.*$/", $status, $matches)){ if(intval($matches[1]) / 100 == 2){ return $body; } } log::write("[{$url2["host"]}] body is null"); return false; }