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',
            )
        );
    }



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章