Showing posts with label dead. Show all posts
Showing posts with label dead. Show all posts

Wednesday, July 25, 2012

Broken dead links checker php solution

If you want to know how to check for broken or dead links using php, following is a simple script that uses curl to check broken links, you can run it on your database if you have a table of links that you want to check for possible broken links:


            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HEADER, true);
            curl_setopt($ch, CURLOPT_NOBODY, true);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $data = curl_exec($ch);
            curl_close($ch);

This sets nobody=true which means it doesn't request for the whole page hence speeding up the link checking. There are plenty of ways within curl to check for broken or redirecting links, I personally use this by putting a simple length check on data variable, if its 0 then it means the link is dead.

i.e.
if(strlen($data)==0)
   echo 'dead link=>'.$url;

You can do something else if you like.

Cheers.