php异步实现,避免长时间等待

处理的php异步的方法有好几种,这里我就只介绍我经常用的而且官方也推荐的

废话少说,直接贴代码

//php异步
    public function doRequest($host,$path, $param=array()){
        $query = isset($param)? http_build_query($param) : ''; 
 
        $port = 80; 
        $errno = 0; 
        $errstr = ''; 
        $timeout = 10; 
 
        $fp = fsockopen($host, $port, $errno, $errstr, $timeout); 
 
        $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); 
    }

//调用实例:(我这里值介绍POST方式,GET方便那么简单就不介绍了,一样的

    //参数说明:参数1[请求目标地址的主域名],参数2[路劲,一般是"入口文件/模块/控制器/操作方法",当然也不排除你的单个php文件访问,后面就是你要进行传递的数据了了]
    public function ybutest(){
        $this->doRequest('www.cms.com','/api.php/User/Users/login',array(
            'username'=>'test001',
            'pwd'=>'123456',
            'service_type'=>1,
            'call_back_rul'=>'http://www.dbtool.com/index.php/Home/Index/test_write',
            )
        );
    }

转载自:https://blog.csdn.net/df981011512/article/details/73866340