fsockopen 非阻塞 post

未结帖
0 2654
ajian admin_jim 2020-05-12
悬赏:5飞吻

public  function asyncRequest($host, $port,$path, $param = array()){


        $query = isset($param) ? http_build_query($param) : '';

        

        $errno = 0;

        $errstr = '';

        $timeout = 30; //连接超时时间(S)

        $port = $port?$port:80;

        $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 " . $path . " 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\r\n";

        $out .= $query;


        $result = @fputs($fp, $out);


        @fclose($fp);

        return $result;


    }

    public  function asyncRequestHttps($host, $port,$path, $param = array()){


        $query = isset($param) ? http_build_query($param) : '';

        

        $errno = 0;

        $errstr = '';

        $timeout = 30; //连接超时时间(S)

        $port = $port?$port:443;

        $fp = @fsockopen('ssl://'.$host, $port, $errno, $errstr, $timeout);


        if (!$fp) {


            return 'ssl连接失败';

        }

        if ($errno || !$fp) {


            return $errstr;

        }


        stream_set_blocking($fp,0); //非阻塞

        stream_set_timeout($fp, 1);//响应超时时间(S)

        $out  = "POST " . $path . " 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\r\n";

        $out .= $query;


        $result = @fputs($fp, $out);


        @fclose($fp);

        return $result;


    }


热忱回答0


最近热帖

近期热议

  1. javascript——prototype与__proto 9
  2. Mysql 中出现的Data truncated for column 3
  3. 在nginx中使用x-sendfile的解决方案 3
  4. 高版本jQuery面插件实现Ajax上传图片 1
  5. Thinkphp Socket.class.php 类的使用 1
  6. 使用ionic3创建第一个App 0
  7. ios-oc html5 0
  8. nginx.conf 0
  9. 基于ionic3.4.0的项目搭建 0
  10. php 缩略图 0