Article From:https://www.cnblogs.com/IT--Loding/p/9063568.html

  In normal cases, PHP is a synchronous request, and the script is executed on the right and down, and the next step is required to make the next operation. This efficiency is not necessary at some time, such as sending mail and other operations, it can be handled asynchronously.

  PHPAsynchronous is also quite a lot of plug-ins. We are using native PHP functions, fsockopen (), modifying function HTTP requests and socket.

  The official website Manual of this function has given an example. I post my optimized code, using HTTP.

  

public function dealdata(Request $request)
    {
        $url = 'http://test.com/demo/index/temp';
        $param = [
            'param1'=>'name',
            'param2'=>'value'
        ];
        $this->syncRequest($url, $param);
    }

    public function syncRequest($url, $param=array(),$timeout =10)
    {
        $urlParmas = parse_url($url);
        $host = $urlParmas['host'];
        $path = $urlParmas['path'];
        $port = isset($urlParmas['port'])? $urlParmas['port'] :80;
        $errno = 0;
        $errstr = '';

        $fp = fsockopen($host, $port, $errno, $errstr, $timeout);
//        stream_set_blocking($fp,0);
        $query = isset($param)? http_build_query($param) : '';
        $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;

        fputs($fp, $out);
        fclose($fp);
    }

 

Similar Posts:

Link of this Article: PHP asynchronous request

Leave a Reply

Your email address will not be published. Required fields are marked *