How do I make an asynchronous GET request in PHP?
file_get_contents
will do what you want
$output = file_get_contents('http://www.example.com/');echo $output;
Edit: One way to fire off a GET request and return immediately.
Quoted from http://petewarden.typepad.com/searchbrowser/2008/06/how-to-post-an.html
function curl_post_async($url, $params){ 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); $out = "POST ".$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"; if (isset($post_string)) $out.= $post_string; fwrite($fp, $out); fclose($fp);}
What this does is open a socket, fire off a get request, and immediately close the socket and return.
This is how to make Marquis' answer work with both POST and GET requests:
// $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); }
Regarding your update, about not wanting to wait for the full page to load - I think a HTTP HEAD
request is what you're looking for..
get_headers should do this - I think it only requests the headers, so will not be sent the full page content.
"PHP / Curl: HEAD Request takes a long time on some sites" describes how to do a HEAD
request using PHP/Curl
If you want to trigger the request, and not hold up the script at all, there are a few ways, of varying complexities..
- Execute the HTTP request as a background process, php execute a background process - basically you would execute something like
"wget -O /dev/null $carefully_escaped_url"
- this will be platform specific, and you have to be really careful about escaping parameters to the command - Executing a PHP script in the background - basically the same as the UNIX process method, but executing a PHP script rather than a shell command
- Have a "job queue", using a database (or something like beanstalkd which is likely overkill). You add a URL to the queue, and a background process or cron-job routinely checks for new jobs and performs requests on the URL