php fsockopen实现异步请求

2/10/2017来源:ASP.NET技巧人气:1270

首先需要php支持fsockopen php.ini文件中查找

allow_url_fopen = On public function requestBySock($data){ $len = strlen($data); $host = 'localhost'; $path = '/test.php'; $fp = fsockopen($host , 80, $errno, $errstr, 3); if ($fp) { $out = "POST $path HTTP/1.1\r\n"; $out .= "Host: $host\r\n"; $out .= "Content-type: application/x-www-form-urlencoded\r\n"; $out .= "Content-Length: $len\r\n"; $out .= "Connection: close\r\n"; //$out .= "Expect:\r\n"; $out .= "\r\n"; $out .= $data."\r\n"; fwrite($fp, $out); //实现异步把下面注释掉,意思是不处理返回 // $receive = ''; // while (!feof($fp)) { // $receive .= fgets($fp, 128); // } // echo "<br />".$receive; //连接主动断开时,线上PRoxy层没有及时把请求发给上游 usleep(10000); fclose($fp); } }