Tutorial – Check for a 404 with PHP

This tutorial will go over how to check for a 404 page with PHP. In other words this will allow you to check if the page you are linking to or trying to get information from is a valid page. Here is the code before I break it down:

In my code I am simply checking to see if my twitter page is a valid URL. If you were to add an extra character anywhere in “heckdesigns” such as an extra “s” at the end then you would trigger the invalid URL message.

Now lets see how that is done:

$ch = curl_init('http://twitter.com/heckdesigns'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

What this is doing is initializing http://twitter.com/heckdesigns and then it is setting cURL option CURLOPT_RETURNTRANSFER to true. This is simply taking all the information sent by the URL (such as HTTP codes) and converting it into a text string we can pull information from later on in the code. To read up on what options you can add to curl_setopt() you can visit php.net.

Lets take a look at that line that will execute the options we’ve set:

curl_exec($ch);

This is simply executing the cURL that was initialized by curl_init, again instead of returning the URL this will instead return it as a string.

Now that we have the information from the URL lets look at the code that will actually interpret that string and see if there is a 404 error in it.

if(curl_getinfo($ch, CURLINFO_HTTP_CODE) == 404) {
	echo 'not a valid url';
}else {
	echo 'valid url';
}

This is getting the information from the $ch string, specifically it is looking for the CURLINFO_HTTP_CODE and checking if it is 404. If it is then it simply returns that is it not a valid URL otherwise it returns that it is a valid URL. If you would like to learn about what other options of information you can retrieve with curl_getinfo take a look at the list on php.net.

Now lets take a look at the last part of the code:

curl_close($ch);

This is simply cleaning up the cURL session by closing the initialized cURL session, in this case $ch.

Leave a Reply