Skip to content

Instantly share code, notes, and snippets.

@flesch
Created March 15, 2011 15:47
Show Gist options
  • Save flesch/870910 to your computer and use it in GitHub Desktop.
Save flesch/870910 to your computer and use it in GitHub Desktop.
Non-blocking background HTTP request
<?php
function async_request($url) {
$request = parse_url($url);
if (!isset($request['port'])) {
$request['port'] = 80;
}
$fp = @fsockopen($request['host'], $request['port'], $errno, $errstr, 30);
if ($fp) {
fwrite($fp, implode("\r\n", array(
sprintf('GET %s HTTP/1.1', $request['path']),
sprintf('Host: %s', $request['host']),
'Connection: Close'
)) . "\r\n\r\n");
fclose($fp);
return true;
} else {
return false;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment