PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I have an array of about 50 URLs. All I'm trying to do is see if the link is a valid link. Is there a more effecient way to handle this? It works, I just think it takes too long. (When I say valid I mean does the link work. I'm not worried about does it contain "http" or "https" or "www")
I'd use curl, and just ask it to return the headers. I'd set the timeout pretty low, adding any that fail to a retry array, so you can make sure by retrying those once or twice before saying they don't exist..
<?php
foreach($a_bookmarks as $val ){
// create a new curl resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $val);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
if(!curl_exec($ch))
$this->a_link_status[] = FALSE;
else
$this->a_link_status[] = TRUE;
// close curl resource, and free up system resources
curl_close($ch);
}
?>
My Questons:
1. I'm able to get the headers but the whole page is still being called. How do I call the headers only?
2. There are a couple different timeout options Which one should I use? I'm thinking CURLOPT_TIMEOUT is the correct one?
3. Using the above the curl_exec does not return false if the link is bad.
<?php
foreach($a_bookmarks as $val ){
// create a new curl resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $val);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT,2);
curl_setopt($ch, CURLOPT_NOBODY,1);
//What do I do here? How do I verify if the headers are legit?
curl_exec($ch);
// close curl resource, and free up system resources
curl_close($ch);
}
?>