Skip to content

Instantly share code, notes, and snippets.

@lineker
Created March 9, 2012 00:22
Show Gist options
  • Save lineker/2004309 to your computer and use it in GitHub Desktop.
Save lineker/2004309 to your computer and use it in GitHub Desktop.
PHP: Check if URL is available
//check if url is available
function is_available($url, $timeout = 30) {
$ch = curl_init(); // get cURL handle
// set cURL options
$opts = array(CURLOPT_RETURNTRANSFER => true, // do not output to browser
CURLOPT_URL => $url, // set URL
CURLOPT_NOBODY => true, // do a HEAD request only
CURLOPT_TIMEOUT => $timeout); // set timeout
curl_setopt_array($ch, $opts);
curl_exec($ch); // do it!
$retval = curl_getinfo($ch, CURLINFO_HTTP_CODE) == 200; // check if HTTP OK
curl_close($ch); // close handle
return $retval;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment