
public function asyncRequest($host, $port,$path, $param = array()){
$query = isset($param) ? http_build_query($param) : '';
$errno = 0;
$errstr = '';
$timeout = 30; //连接超时时间(S)
$port = $port?$port:80;
$fp = @fsockopen($host, $port, $errno, $errstr, $timeout);
//$fp = stream_socket_client("tcp://".$host.":".$port, $errno, $errstr, $timeout);
if (!$fp) {
return '连接失败';
}
if ($errno || !$fp) {
return $errstr;
}
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;
}
public function asyncRequestHttps($host, $port,$path, $param = array()){
$query = isset($param) ? http_build_query($param) : '';
$errno = 0;
$errstr = '';
$timeout = 30; //连接超时时间(S)
$port = $port?$port:443;
$fp = @fsockopen('ssl://'.$host, $port, $errno, $errstr, $timeout);
if (!$fp) {
return 'ssl连接失败';
}
if ($errno || !$fp) {
return $errstr;
}
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;
}