Created
March 27, 2014 13:54
-
-
Save carbontwelve/9808078 to your computer and use it in GitHub Desktop.
PHP Curl to check if url is alive
This file contains 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
<?php | |
function check_alive($url, $timeout = 10, $successOn = array(200, 301)) { | |
$ch = curl_init($url); | |
// Set request options | |
curl_setopt_array($ch, array( | |
CURLOPT_FOLLOWLOCATION => false, | |
CURLOPT_NOBODY => true, | |
CURLOPT_TIMEOUT => $timeout, | |
CURLOPT_USERAGENT => "page-check/1.0" | |
)); | |
// Execute request | |
curl_exec($ch); | |
// Check if an error occurred | |
if(curl_errno($ch)) { | |
curl_close($ch); | |
return false; | |
} | |
// Get HTTP response code | |
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
curl_close($ch); | |
// Page is alive if 200 OK is received | |
//return $code; | |
return in_array( $code, $successOn ); | |
} | |
$checks = array( | |
'http://www.google.co.uk', | |
'http://www.facebook.com', | |
'http://www.bbc.co.uk', | |
'http://photogabble.co.uk', | |
'http://youtube.com' | |
); | |
foreach($checks as $check) | |
{ | |
echo $check . ' is ' . ( (check_alive($check) ) ? 'Alive' : 'Dead' ) . "\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
for some reason it don't pick up 404 as an error