curl socket 访问_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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值