php http_build_query异步(模拟多线程)

if (!function_exists('php_get_')) {
    function php_get_($url='/index/index/get_add',$param){
        $host = $_SERVER['HTTP_HOST'];
        $port = 80;
        $errno = '';
        $errstr = '';
        $timeout = 30;
        $url = $url.'?'.http_build_query($param);
        // create connect
        
        $fp = fsockopen($host, $port, $errno, $errstr, $timeout);
        if(!$fp){
            return false;
        }
      
        $out = "GET ".$url." HTTP/1.1\r\n";
        $out .= "Host:".$host."\r\n";
        $out .= "Connection:close\r\n";
        $out.="\r\n";
        fwrite($fp, $out);
        //忽略执行结果;否则等待返回结果
         // if(false){
        //     $ret = '';
        //     while (!feof($fp)) {
        //         $ret .= fgets($fp, 128);
        //     }
        // }
        usleep(20000); //fwrite之后马上执行fclose,nginx会直接返回499
        fclose($fp);
      // if ($ret != '') {
      //     $start = strpos($ret, '<?xml');
      //     if ($start > 0) {
      //         $ret = substr($ret, $start);
      //     }
      // }
      // return $ret;
  }
}
if (!function_exists('php_post_')) {
  function php_post_($url, $param){
      $host = $_SERVER['HTTP_HOST'];
      $query = isset($param) ? http_build_query($param) : '';
        $port = 80;
        $errno = 0;
        $errstr = '';
        $timeout = 30; //连接超时时间(S)
        $fp = @fsockopen($host, $port, $errno, $errstr, $timeout);
        //$fp = stream_socket_client("tcp://".$host.":".$port, $errno, $errstr, $timeout);
        if (!$fp) {
            return '连接失败';
        }
        if ($errno || !$fp) {
            return $errstr;
        }
 
        stream_set_blocking($fp,0); //非阻塞
        stream_set_timeout($fp, 1);//响应超时时间(S)
        $out  = "POST " . $url . " HTTP/1.1\r\n";
        $out .= "host:" . $host . "\r\n";
        $out .= "content-length:" . strlen($query) . "\r\n";
        $out .= "content-type:application/x-www-form-urlencoded\r\n";
        $out .= "connection:close\r\n";
        $out.="\r\n";
        $out .= $query;
        $result = @fwrite($fp, $out);
        
        usleep(20000);
        @fclose($fp);
        return $result;
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值