正常狀況下,PHP都是同步請求,腳本右上而下依次執行,必須等上一步請求好了,才能進行下一步操做,這種效率在某些時候是沒必要要的,如發送郵件等操做,是能夠異步處理的。app
PHP異步也很很多插件,咱們使用的是原生的PHP函數,fsockopen(),改函數http請求和socket均可以。異步
這個函數官網手冊已經給出了示例,我貼出我優化後的代碼,使用的是http。socket
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); }