-
-
Save tosunkaya/20e36a61bbba92012c53e9b514436b15 to your computer and use it in GitHub Desktop.
PHP/cURL function to check a web site status. If HTTP status is not 200 or 302, or the requests takes longer than 10 seconds, the website is unreachable. See https://www.saotn.org/php-curl-check-website-availability/.
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
<?php | |
/** | |
* PHP/cURL function to check a web site status. If HTTP status is not 200 or 302, or | |
* the requests takes longer than 10 seconds, the website is unreachable. | |
* | |
* Follow me on Twitter: @HertogJanR | |
* | |
* @param string $url URL that must be checked | |
*/ | |
function url_test($url) { | |
$timeout = 10; | |
$ch = curl_init(); | |
curl_setopt ($ch, CURLOPT_URL, $url); | |
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt ($ch, CURLOPT_TIMEOUT, $timeout); | |
$http_respond = curl_exec($ch); | |
$http_respond = trim(strip_tags($http_respond)); | |
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
if (($http_code == "200") || ($http_code == "302")) { | |
return true; | |
} else { | |
// return $http_code;, possible too | |
return false; | |
} | |
curl_close($ch); | |
} | |
$website = "www.example.com"; | |
if(!url_test($website)) { echo $website ." is down!"; } | |
else { echo $website ." functions correctly."; } | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment