码迷,mamicode.com
首页 > 其他好文 > 详细

fsocket发送post实现异步请求

时间:2015-08-04 20:35:46      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:

 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\r\n";
18         $header .= "Host: ". $url_array[‘host‘] . "\r\n"; //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\r\n";//POST数据
25                 $header   .= "Content-Length: ". strlen($_post) ." \r\n";//POST数据的长度
26                 $header      .= "Connection:Close\r\n\r\n";
27 
28                 $header .= $_post."\r\n\r\n "; //传递POST数据
29         }
30 
31 
32         fwrite($fp, $header);
33         //echo fread($fp, 1024); //我们不关心服务器返回
34         fclose($fp);
35         return true;
36 }

 

fsocket发送post实现异步请求

标签:

原文地址:http://www.cnblogs.com/hanyouchun/p/4702808.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!