• 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 }
  • 相关阅读:
    windows 7 wifi热点配置
    Java中的try catch finaly先后调用顺序
    redis php扩展
    mysql索引
    cmd操作数据库的常用命令
    php ajax解决跨越问题
    git常用命令
    php,redis分布式锁防并发
    php商城下单,可以购买多件商品,redis防高并发
    php商城秒杀,redis防高并发
  • 原文地址:https://www.cnblogs.com/hanyouchun/p/4702808.html
Copyright © 2020-2023  润新知