fsocket发送post实现异步请求
2015-08-04 18:33
483 查看
function triggerRequest($url, $post_data = array(), $cookie = array()){ //可以通过POST或者GET传递一些参数给要触发的脚本 $url_array = parse_url($url); //获取URL信息,以便平凑HTTP HEADER $port = isset($url_array['port'])? $url_array['port'] : 80; $fp = fsockopen($url_array['host'], $port, $errno, $errstr, 30); if (!$fp){ return FALSE; } $getPath = $url_array['path']; isset($url_array['query']) && $getPath.= "?". $url_array['query']; $method = empty($post_data) ? "GET":"POST"; $header = $method . " " . $getPath; $header .= " HTTP/1.1\r\n"; $header .= "Host: ". $url_array['host'] . "\r\n"; //HTTP 1.1 Host域不能省略 if(!empty($post_data)){ $_post = http_build_query($post_data); $header .= "Content-Type: application/x-www-form-urlencoded\r\n";//POST数据 $header .= "Content-Length: ". strlen($_post) ." \r\n";//POST数据的长度 $header .= "Connection:Close\r\n\r\n"; $header .= $_post."\r\n\r\n "; //传递POST数据 } fwrite($fp, $header); //echo fread($fp, 1024); //我们不关心服务器返回 fclose($fp); return true; }
相关文章推荐
- ceph存储 strace命令用法详解
- web.xml 指定配置路径
- 直到开始找工作才发现自己基础多差。现在追赶还来得及!开始行动吧!
- strlen函数实现的两种方法
- Activity Lifecycle
- Android之Adapter总结
- 【转】终极 Web 应用性能和压力测试工具 Gor
- Codeforces gym 100685 C. Cinderella 水题
- Linq List<T>.Distinct() 去除对象重复
- 【LIS】【严格递增和非严格递增】
- ubuntu命令
- 正则表达式验证日期
- android检查版本更新实现思路
- git Bash 安装&配置与基本操作
- git Bash 安装&配置与基本操作
- Bootstrap的js插件之滚动监听(scrollspy)
- avl树的插入(含单旋转,双旋转)
- C# string Spilt
- 在meteor中使用支付,以及与服务器进行数据交互
- jq尺寸和位置总结篇(width scrollTop position offset)