fsockopen()
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
die('error fsockopen');
}
// 转换到非阻塞模式
stream_set_blocking($fp, 0);
$http = "GET /save.php / HTTP/1.1\r\n";
$http .= "Host: www.example.com\r\n";
$http .= "Connection: Close\r\n\r\n";
fwrite($fp, $http);
fclose($fp);
$fp = fsockopen($host,$port,$error_code,$error_msg,1);
if(!$fp) {
return array('error_code' => $error_code,'error_msg' => $error_msg);
}
else {
stream_set_blocking($fp,true);//开启了手册上说的非阻塞模式
stream_set_timeout($fp,1);//设置超时
$header = "GET $path HTTP/1.1\r\n";
$header.="Host: $host\r\n";
$header.="Connection: close\r\n\r\n";//长连接关闭
fwrite($fp, $header);
usleep(1000); // 这一句也是关键,如果没有这延时,可能在nginx服务器上就无法执行成功
fclose($fp);
return array('error_code' => 0);