PHP Function to check URL for 404

I found myself writing up some code for a website application that needed to display a slider of images for different inventory. Now, there is a ridiculous amount of inventory (thousands and thousands) and the images are not stored in a DB. Likewise, not all of them were made equal (read: some inventory has alot of pictures, some have none, and there are many in between).

Now, also interestingly enough, the images are stored on a separate server.. so a simple file check was out of the question. This left me with using a display program that was either forced to only display a few pictures so that most of them would not hang upon not being able to find inventory_xxxx.jpg

To solve this situation I had to put together this simple PHP function that simply returns a Boolean, true if the link is valid, false if some error is encountered instead. Of course, that may seem a little backward, so here is a slightly modified version that simply “checks the url for a 404 error code” and return false if it does, otherwise true.


<?php
function checkurl($url)
{
    // Simple check
    if (!$url)
    {
        return FALSE;
    }

    // Create cURL resource using the URL string passed in
    $curl_resource = curl_init($url);

    // Set cURL option and execute the "query"
    curl_setopt($curl_resource, CURLOPT_RETURNTRANSFER, true);
    curl_exec($curl_resource);

    // Check for the 404 code (page must have a header that correctly display 404 error code according to HTML standards
    if(curl_getinfo($curl_resource, CURLINFO_HTTP_CODE) == 404)
    {
        // Code matches, close resource and return false
        curl_close($curl_resource);
        return FALSE;
    }
    else
    {
        // No matches, close resource and return true
        curl_close($curl_resource);
        return TRUE;
    }

    // Should never happen, but if something goofy got here, return false value
    return FALSE;
}
?>

Ideally, a better version would probably be a container function for a similar function which actually return the error codes or a NULL/FALSE value for a URL which returns no code.

For my purposes, this works perfectly and I make sure that the URLs I am checking against are located on a server where I control whether or not the 404 page send a correct 404 header back to the client.

Either way, enjoy and I hope its of use.

2 Comments
  1. Reply Wisher March 14, 2013 at 05:13

    From where the Function wil be called be callled …….

    you have only define the checkurl($url) function.

  2. Reply Izzy May 9, 2013 at 16:54

    Sorry for the late reply, basically you can call this function from anywhere as long as you have this code included or defined somewhere else in your code.

    An example would be:

    if (checkurl(‘http://www.domain.com/image.jpg’))
    {
    echo ‘<img src=”http://www.domain.com/image.jpg”>’;
    }
    else
    {
    // .. do something or display something else
    }

Leave a Reply