Check Dead Links From Database Using PHP CURL
Check Dead/Broken Links From Database Using PHP CURL:
Hola Friends ! Checking Deadlinks From the database manually is a Headache ,So why not use a script which return the http status of the particular link and tell us if the link is dead or not.So how do we check the dead links from the database ? How do we programatically check whether the link is dead or not ?
To check broken or dead links from Database we will use curl .Curl is PHP library which can make head HTTP head requests which are good enough to find if a link is broken/dead or not.So why not we use CURL rather than fopen which uses more bandwidth.CURL saves our bandwidth and time.
So I have made a Script using PHP curl which check the deadlinks,which is Quite simple and easy.You can use this for checking single link or Use a for loop to iterate through the link from your database.So lets take a look at script which checks dead links from your database and iterate through each link and send a curl request to check whether link is invalid or not.
PHP Script To Check For Dead Links From Database:
Here is the Php function which check the dead links using curl.you could call it by supplying your url to this function and this function will return true if the link is online and false if link is dead.You can use this function to check the dead link .This can be used to check single link or multiple links.
$ch = curl_init();
// set cURL options
$opts = array(CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => $link, // your URL here
CURLOPT_NOBODY => true, // do a HEAD request only
CURLOPT_TIMEOUT => $tout); // set timeout
curl_exec($ch); // do it!
$output = curl_getinfo($ch, CURLINFO_HTTP_CODE) == 200; // check if HTTP OK
curl_close($ch); // close handle
That was easy ! now let me show you how to use this with multiple link from the database.
Check Invalid/Dead Links From Database :
Now we’ll use the same above function to check dead links from the database .We will iterate through the Links in our database . So we will connect to database collect all the links and iterate through all the links and if the link is dead then we store the link id in the array.that’w how we find all the dead links from the database.
$sql="select id,link from videos";
$is_dead=checklink($row['link']);//call te fucntion and pass it the link
$deadLinks=$row['id'];//array will hold all deadlink id's
That is how we Check all our dead links from the database using the curl library of the php.Also when this script run on server rather than desktop it will be much more faster.If you have 1000 or more link then it might give timeout error on localhost(xampp,wamp) , so to try it on the localhost use the limit ,then it will work fine.But on server it will work fine whether you have 1000 or 5000 links.
I hope You likes this Script ,Thanks for visiting on Design Aeon and keep visiting.