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);

curl_exec($ch);
 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
 if($httpCode == 404) {
 // send the text
 mail('phonenumber@carrierdomain.com', 'Page is down', 'The page /page-url/ is broken');
 }

curl_close($ch);

Option 2: get_headers():

 if (get_headers($url)[0] == 'HTTP/1.1 404 Not Found') {
 // send text
 mail('phonenumber@carrierdomain.com', '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:
Sprint phonenumber@messaging.sprintpcs.com
Verizon phonenumber@vtext.com
T-Mobile phonenumber@tmomail.net
AT&T phonenumber@txt.att.net

Leave a Reply

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