PHP實現發送非同步要求方法

來源:互聯網
上載者:User
本文主要和大家分享PHP實現發送非同步要求方法,最近在工作中多次碰到需要用到PHP非同步請求的問題,所以在網上尋找了相關的資料。經過多次的測試和修改,總結了兩種普遍可行的的方案:

1、方案一:使用CURL,但必須設定CUROPT_TIMEOUT為1。

function _curl($url, $data=null, $timeout=0, $isProxy=false){    $curl = curl_init();    if($isProxy){   //是否設定代理        $proxy = "127.0.0.1";   //代理IP        $proxyport = "8001";   //代理連接埠        curl_setopt($curl, CURLOPT_PROXY, $proxy.":".$proxyport);    }    curl_setopt($curl, CURLOPT_URL, $url);    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);    if(!empty($data)){        curl_setopt($curl, CURLOPT_POST, 1);        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);        curl_setopt($curl, CURLOPT_HTTPHEADER, array(                "cache-control: no-cache",                "content-type: application/json",)        );    }    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);    if ($timeout > 0) { //逾時時間秒        curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);    }    $output = curl_exec($curl);    $error = curl_errno($curl);    curl_close($curl);    if($error){        return false;    }    return $output;}_curl('http://localhost/index.php',null,1);


2、方案二:使用fsockopen,但需要自己拼出HTTP的header部分

function _fsockopen($url,$post_data=array(),$cookie=array()){$url_arr = parse_url($url);$port = isset($url_arr['port'])?$url_arr['port']:80;if($url_arr['scheme'] == 'https'){$url_arr['host'] = 'ssl://'.$url_arr['host'];}$fp = fsockopen($url_arr['host'],$port,$errno,$errstr,30);if(!$fp) return false;$getPath = isset($url_arr['path'])?$url_arr['path']:'/index.php';$getPath .= isset($url_arr['query'])?'?'.$url_arr['query']:'';$method = 'GET';  //預設get方式if(!empty($post_data)) $method = 'POST';$header = "$method  $getPath  HTTP/1.1\r\n";$header .= "Host: ".$url_arr['host']."\r\n";if(!empty($cookie)){  //傳遞cookie資訊$_cookie = strval(NULL);foreach($cookie AS $k=>$v){$_cookie .= $k."=".$v.";";}$cookie_str = "Cookie:".base64_encode($_cookie)."\r\n";$header .= $cookie_str;}if(!empty($post_data)){  //傳遞post資料$_post = array();foreach($post_data AS $_k=>$_v){$_post[] = $_k."=".urlencode($_v);}$_post = implode('&', $_post);$post_str = "Content-Type:application/x-www-form-urlencoded; charset=UTF-8\r\n";$post_str .= "Content-Length: ".strlen($_post)."\r\n";  //資料長度$post_str .= "Connection:Close\r\n\r\n";$post_str .= $_post;  //傳遞post資料$header .= $post_str;}else{$header .= "Connection:Close\r\n\r\n";}fwrite($fp, $header);//echo fread($fp,1024);usleep(1000); // 這一句也是關鍵,如果沒有這延時,可能在nginx伺服器上就無法執行成功fclose($fp);return true;}_fsockopen('http://localhost/index.php'));

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.