Created
March 9, 2012 00:22
-
-
Save lineker/2004309 to your computer and use it in GitHub Desktop.
PHP: Check if URL is available
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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