Check if a page is 404’ed and send a text message alerting

I was browsing stackoverflow today and I started to write a response to this question “Check if links are 404 and if so, then send sms” and by the time I finished there were already two responses that covered my one! So instead i’m posting it here.
There are two ways I know of to do this:

Option 1: cURL

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_HEADER, 0);
 curl_setopt($ch, CURLOPT_NOBODY, 1);

 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
 if($httpCode == 404) {
 // send the text
 mail('', 'Page is down', 'The page /page-url/ is broken');


Option 2: get_headers():

 if (get_headers($url)[0] == 'HTTP/1.1 404 Not Found') {
 // send text
 mail('', 'Page is down', 'The page /page-url/ is broken');

My preference would be for get_headers() because of how succinct it is. If you are curious for some carrier domains to send text messages to here are a few of the major ones:

Leave a Reply

Your email address will not be published. Required fields are marked *