fsocket发送post实现异步请求

 1 function triggerRequest($url, $post_data = array(), $cookie = array()){
 2           //可以通过POST或者GET传递一些参数给要触发的脚本
 3         $url_array = parse_url($url); //获取URL信息,以便平凑HTTP HEADER
 4         $port = isset($url_array['port'])? $url_array['port'] : 80; 
 5       
 6         $fp = fsockopen($url_array['host'], $port, $errno, $errstr, 30); 
 7         if (!$fp){
 8                 return FALSE;
 9         }
10 
11         $getPath = $url_array['path'];
12         isset($url_array['query']) && $getPath.= "?". $url_array['query'];
13         $method = empty($post_data) ? "GET":"POST";
14   
15 
16         $header = $method . " " . $getPath;
17         $header .= " HTTP/1.1
";
18         $header .= "Host: ". $url_array['host'] . "
"; //HTTP 1.1 Host域不能省略
19 
20      
21         if(!empty($post_data)){
22                 $_post = http_build_query($post_data);
23 
24                 $header   .= "Content-Type: application/x-www-form-urlencoded
";//POST数据
25                 $header   .= "Content-Length: ". strlen($_post) ." 
";//POST数据的长度
26                 $header      .= "Connection:Close

";
27 
28                 $header .= $_post."

 "; //传递POST数据
29         }
30 
31 
32         fwrite($fp, $header);
33         //echo fread($fp, 1024); //我们不关心服务器返回
34         fclose($fp);
35         return true;
36 }
原文地址:https://www.cnblogs.com/hanyouchun/p/4702808.html