zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    jQuery弹出层插件大全:
    JavaScript数组去重的几种方法
    sql去除重复列(行)
    VS无法启动调试
    .将DayOfWeek转换成中文的几种方式
    关于 uniqueidentifier
    链接服务器
    我的目标:系统架构师
    异常(1)
    Visual C++开发工具与调试技巧整理
  • 原文地址:https://www.cnblogs.com/hanyouchun/p/4702808.html
Copyright © 2011-2022 走看看