PHP 中的非同步GET 請求
在PHP 中發出非同步GET 請求可以與外部腳本進行高效通信,而不會阻塞主線程。
使用file_get_contents()
對於不需要輸出的GET 請求,可以使用file_get_contents():
$output = file_get_contents('http://www.example.com/');
function make_async_get($url) { $parts = parse_url($url); $fp = fsockopen($parts['host'], isset($parts['port']) ? $parts['port'] : 80, $errno, $errstr, 30); $out = "GET ".$parts['path']." HTTP/1.1\r\n"; $out.= "Host: ".$parts['host']."\r\n"; $out.= "Connection: Close\r\n\r\n"; fwrite($fp, $out); fclose($fp); } make_async_get('http://www.externalsite.com/script1.php?variable=45');注意:
如果您需要 GET 請求的輸出,請考慮使用curl_post_async(),如引用的解決方案所示。
以上是如何在 PHP 中發出非同步 GET 請求?的詳細內容。更多資訊請關注PHP中文網其他相關文章!