PHP非同步處理方法____PHP

來源:互聯網
上載者:User
PHP四種常用的非同步處理解決方案 1、使用前端Ajax處理
$.ajax("do.php", { name: 'ityangs',job:'PHP Programmer'} );
2、使用popen函數執行本地檔案
pclose(popen('php /var/www/do.php &', 'r'));
3、使用CURL

設定curl的逾時時間 CURLOPT_TIMEOUT 為1 (最小為1),因此用戶端需要等待1秒,curl請求地址必須為絕對路徑

$param = array('name'=>'ityangs','job'=>'PHP Programmer');$ch = curl_init();curl_setopt($ch, CURLOPT_URL,"http://www.example.com/do.php");curl_setopt($ch, CURLOPT_POST, 1);curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($param)); //將數群組轉換為URL請求字串curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_HEADER, false);curl_setopt($ch, CURLOPT_TIMEOUT, 1);curl_exec($ch);curl_close($ch);
4、使用fsockopen函數,需要自己拼接header部分
function doRequest($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);$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);}$url = 'http://www.example.com/do.php';$param = array('name'=>'ityangs','job'=>'PHP Programmer');doRequest($url, $param);

注意:
1、如果使用Apache作為Web伺服器,讓PHP支援非同步首先必須得在Apache設定檔httpd.conf配置enablesendfile
on。
2、在非同步執行的PHP檔案中建議加上一下兩個配置:

ignore_user_abort(true); // 忽略用戶端斷開set_time_limit(0); // 設定執行不逾時
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.