如何在PHP中發(fā)出異步GET請求?我希望向另一個服務(wù)器上的另一個腳本發(fā)出一個簡單的GET請求。我該怎么做?在一種情況下,我只需要請求一個外部腳本,而不需要任何輸出。make_request('http://www.externalsite.com/script1.php?variable=45'); //example usage在第二種情況下,我需要得到文本輸出。$output = make_request('http://www.externalsite.com/script2.php?variable=45');echo $output; //string output老實說,我不想亂搞卷發(fā),因為這真的不是卷發(fā)的工作。我也不想使用http_get,因為我沒有PECL擴展。fsockopen會起作用嗎?如果是這樣,我如何在不讀取文件內(nèi)容的情況下做到這一點?沒有別的辦法了嗎?謝謝大家更新我應(yīng)該補充說,在第一種情況下,我不想等待腳本返回任何東西。據(jù)我理解,file_get_content()將等待頁面完全加載,等等?
2 回答

侃侃無極
TA貢獻2051條經(jīng)驗 獲得超10個贊
// $type must equal 'GET' or 'POST' function curl_request_async($url, $params, $type='POST') { foreach ($params as $key => &$val) { if (is_array($val)) $val = implode(',', $val); $post_params[] = $key.'='.urlencode($val); } $post_string = implode('&', $post_params); $parts=parse_url($url); $fp = fsockopen($parts['host'], isset($parts['port'])?$parts['port']:80, $errno, $errstr, 30); // Data goes in the path for a GET request if('GET' == $type) $parts['path'] .= '?'.$post_string; $out = "$type ".$parts['path']." HTTP/1.1\r\n"; $out.= "Host: ".$parts['host']."\r\n"; $out.= "Content-Type: application/x-www-form-urlencoded\r\n"; $out.= "Content-Length: ".strlen($post_string)."\r\n"; $out.= "Connection: Close\r\n\r\n"; // Data goes in the request body for a POST request if ('POST' == $type && isset($post_string)) $out.= $post_string; fwrite($fp, $out); fclose($fp); }

白板的微信
TA貢獻1883條經(jīng)驗 獲得超3個贊
關(guān)于您的更新,關(guān)于不希望等待整個頁面加載-我認為是HTTPHEAD
請求是你要找的.。
獲取報頭應(yīng)該這樣做-我認為它只是請求標題,所以不會發(fā)送完整的頁面內(nèi)容。
PHP/Curl:Head請求在某些站點上需要很長時間描述如何執(zhí)行HEAD
使用PHP/Curl的請求
如果您想觸發(fā)請求,而根本不延遲腳本,那么有幾種方法,具有不同的復(fù)雜性。
- 將HTTP請求作為后臺進程執(zhí)行,-這將是具體的平臺,你必須做到
真的
小心將參數(shù)轉(zhuǎn)義到命令 - -基本上與UNIX進程方法相同,但執(zhí)行PHP腳本而不是shell命令
- 2 回答
- 0 關(guān)注
- 1038 瀏覽
添加回答
舉報
0/150
提交
取消