Check website status using php and CURL library
Many times in our webmaster activity we need some kinda tool that can monitor the activity on our website(s). Me personally I would like to know how much my website is down. It’s extremely important , and really our websites shouldn’t be down much; not at all. Hosting companies are very numerous our days. Can always pick another one better than the existing, one that provides a better uptime.
There are lot’s of services over the WWW where you can pay to have your site monitored and be notified when this is down. The nasty thing here is that they get good money for a serivce that you can easily offer to yourself with a minimum effort.
So let ’s see what’s to be done!
In the article here I chosen to use the CURL PHP library instead of SOCKETS connections. Personally I don’t like sockets in php and I don’t see why we should use them when we don’t really need and when there is another nice way of doing things. CURL is really nifty, can do awesome things. But I am not gonna talk about that here, maybe in a future article
The whole idea here…
Is to have a PHP script(that can be eventually set as a CRON Linux CRON or scheduled task on WIN systems) that would try to open the site just like you do in your browser. If connection times-out than something is wrong obviously. Also, as stated into the standards, there is a HTTP response send back. For a complete reference of HTTP status codes see the HTTP protocol description article.
To accomplish that I chose to use the CURL PHP extension that me personally I like it very much since is a pretty flexible one, that can do so powerful things without too much trouble (I could mention here the easiness of getting the returned HTTP status of a connection that, as presented on other resources using sockets, would mean manually parse the response headers). Basicly, as stated in W3C standards, a successful connection is indicated by a 2XX HTTP code. So the presented script here will try to load the site and compare the returned status with 2XX.
function Visit($url)
{
$agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";$ch=curl_init();
curl_setopt ($ch, CURLOPT_URL,$url );
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch,CURLOPT_VERBOSE,false);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$page=curl_exec($ch);
//echo curl_error($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($httpcode>=200 && $httpcode<300) return true;
else return false;
}
if(Visit("http://www.domain.com"))
echo "Website OK"."n";
/*if site down*/
else
echo "Website DOWN";
Hope you enjoy it and why not … find it useful ![]()
RSS/XML
April 6th, 2008 at 1:13 am
This script works like a charm! Also useful if you have a folder on a server using both http and https. This can test if a website can be visited via https first and then redirects if so. The php server variables are useless if the same folder uses both protocols because when you access the site via regular http it will let you and return nothing about https being on
July 20th, 2008 at 8:27 pm
i cant run it! it wrong or my server not support curl
September 23rd, 2008 at 2:24 am
Thanks alot!
November 2nd, 2008 at 6:40 pm
Works great! check it out - http://colerainebb.co.uk - sometimes the main site @boys-brigade.org.uk takes a break, if so the site is redirected to our @blogspot page!
Many thanks!
November 19th, 2008 at 4:55 am
hey there is one problem with this script..it gives you down status if domain is set redirection….looking for this ?
November 19th, 2008 at 4:59 am
like google.com
January 20th, 2009 at 2:06 am
Curl is definitely better than socket, but i dont know how to enable curl in remotely hosted server.
January 20th, 2009 at 2:52 am
I don’t think there’s a way to enable it without the hosting company help…CURL compiles with PHP so the hositng guys have to do it
Claude
February 13th, 2009 at 1:01 am
hi sir
above code is not properly working.when site is down ,how can get red of this error/warning..plz help me
try this site: leasegreatdomains.com
2:30 PM Warning: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution in /home/brains1/public_html/check.php on line 7
Warning: fsockopen() [function.fsockopen]: unable to connect to leasegreatdomains.com:80 (php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution) in /home/brains1/public_html/check.php on line 7
Website DOWN Down!
February 13th, 2009 at 1:20 am
Try turnning of error reporting ( error_reporting(0) ) or put an @ sign in front of function call.
@fsockopen
Claude
May 5th, 2009 at 12:19 am
Sir When i run the above code its showing the following error.
Fatal error: Call to undefined function curl_init() in C:\projects\site_availability_status\status.php on line 18
May 5th, 2009 at 12:33 am
it’s because you don’t have curl enabled in your php.ini.
Contact your host and they will do that for you
June 7th, 2009 at 5:37 am
To check status of website which is redirected
Change
if($httpcode>=200 && $httpcode
June 20th, 2009 at 10:23 pm
can this code test for https sites?
as i tried it and it gives me this
“SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed”
any way to get this working?
July 19th, 2009 at 1:28 am
Jason,
To get around the SSL certificate stuff, what I do is add these lines:
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch,CURLOPT_SSLVERSION,3);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, FALSE);
September 17th, 2009 at 2:23 pm
Works great, i have used the code in a ddl auto submitter script to see if the sites are online even before submission. Thanks
October 23rd, 2009 at 3:02 am
Hey this code is good i tried with https but it is showing the website is down every time(say https://www.google.com/accounts/ServiceLogin?service=mail) why it is showing like that
November 9th, 2009 at 5:13 am
Any idea why this only works on websites hosted on the same server? When trying websites on other servers it just puts them as “website down”
November 9th, 2009 at 5:29 am
@tester
it’s probalby a certificate problem. you can disable it byu using
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
or the hard way, poingint to right location
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, getcwd() . “/path/to/crt”);
November 9th, 2009 at 5:32 am
@Peder Johnsen
since it works on locl website, I would guess there’s a DNS problem on that server. IE. curl cannot get dns for a certain domain name. but you need to debug to be sure
Best
Claude
November 9th, 2009 at 2:59 pm
@The master
Ok!
Well, I put the script on the server I was gonna have it in the first place, and it works there so! :).
February 8th, 2010 at 2:19 am
I tried to run the script but all the results i am getting is “WEBSITE DOWN” although when i visit the site/domain, the site is up. How come? is there something i am missing here?
February 18th, 2010 at 6:06 am
Thanks, your code helpe me a lot
April 18th, 2010 at 7:48 am
hi i get the error name lookup timed out
and it always says website DOWN
what todo?
April 18th, 2010 at 8:31 am
@matt maybe curl extension is not enabled or perhaps the website you are testing is reponding slower than how many seconds are set into curl_setopt($ch, CURLOPT_TIMEOUT, 5);
hope this helps
April 18th, 2010 at 4:29 pm
hmm
on the same site
so i need to lower that number?
curl is enabled because it has worked in the past
but since a few days it stoped working
April 19th, 2010 at 1:19 am
5 seconds timeout should be fine.
it hard to tell what’s wrong..
maybe try to echo the $httpdcode var inide code and see what that is…
April 19th, 2010 at 4:19 pm
i did and it said time out…
the script stoped working a few days ago
April 21st, 2010 at 7:35 am
@matt
try running the script from on another server…it might be possible that on hte server you are currently running it, there are some sort of DNS issues for eg and curl cannot lookup your domain name.
I might be wrong but it’s a possibility
April 24th, 2010 at 6:16 pm
it worked when i tryed it on my localhost with curl lib on
what i need to do to fix this problem on the online server?
April 28th, 2010 at 8:11 am
@matt first you need to see what the problem is
see if curl gives any notices, try verbosing the output etc..
hope that helps
April 28th, 2010 at 10:46 am
the following script works perfect ( it doesnt show the images but that is normal i supose?)
on the server i want to run the script..
so what else can it be if Curl is working fine?
curl information
curl
cURL support enabled
cURL Information libcurl/7.20.0 OpenSSL/0.9.8b zlib/1.2.3
May 3rd, 2010 at 8:52 am
try doing a print_r(curl_getinfo($ch)); inside the script. if it takes kinda long to return and you get an array with almost all values 0 than there’s more likely a DNS/network issue on that server.
Claude
May 14th, 2010 at 12:29 am
Helo can i use this script to redirect to another website mirror if main site is down? Ie If website A don’t respond after 10 second then is down so script redirect to website mirror b hosted on other host
Can you post script because i’m a newbie and don’t know ho to make it
Thanks
May 14th, 2010 at 6:03 am
curl is installed in my server..but when i execute the code, it is showing as URL not found on this server.Same code is working from my local server.
I tried with print_r(curl_getinfo($ch)); and it returned as array with almost all values 0.
Could someone help me on this.
September 2nd, 2010 at 7:48 pm
Another easy working way:
September 2nd, 2010 at 7:51 pm
Another easy working way:
if (p___url_exist(’http://www.paiz.ir/’))
{ print(’url exists.’);
} else
{ print(’url doesn\’t exist.’);
};
function p___url_exist($url)
{ $r = $h = null;
$h = curl_init($url);
if (false === $h) { return (false); };
curl_setopt($h, CURLOPT_HEADER, false);
curl_setopt($h, CURLOPT_FAILONERROR, true);
curl_setopt($h, CURLOPT_NOBODY, true);
curl_setopt($h, CURLOPT_RETURNTRANSFER, false);
$r = (curl_exec($h) ? true : false);
curl_close($h);
return ($r);
};