/**
* php异步请求 (全路径:www.a.com/a/b.php)
* @param string $host 主机地址 例如: www.a.com
* @param string $path 路径 例如: /a/b.php
* @param array $param 请求参数
* @param int $port 端口
* @return string
*/
function asyncRequest($host, $path, $param = array(),$port=80){
$query = isset($param) ? http_build_query($param) : '';
$errno = 0;
$errstr = '';
$timeout = 30; //连接超时时间(S)
$fp = @fsockopen($host, $port, $errno, $errstr, $timeout);
if (!$fp) {
//连接失败
return false;
}
if ($errno || !$fp) {
// $errstr
return false;
}
stream_set_blocking($fp,0); //非阻塞
stream_set_timeout($fp, 1);//响应超时时间(S)
$out = "POST " . $path . " HTTP/1.1\r\n";
$out .= "host:" . $host . "\r\n";
$out .= "content-length:" . strlen($query) . "\r\n";
$out .= "content-type:application/x-www-form-urlencoded\r\n";
$out .= "connection:close\r\n\r\n";
$out .= $query;
$result = @fputs($fp, $out);
@fclose($fp);
return $result;
}
网友评论