greets, i'm new here.
as my topic says, i am attemtping to create a function to quickly verify the existence/validity of a URL.
so far i have been trying to pass a single parameter called $target. this is a string variable containing the hyperlink to be tested. in theory this function should return a boolean true if the link exists and a boolean false if it doesn't or isn't accessible.
i have a function which does this coded but my problem is this: if the target doesn't exist, the function takes a really long time to give up and return false.
before i post my current code, i'm curious if anybody has a way to implement this function and keeping its execution time limited to a variable number of seconds (in my impatient case, 1). i have been thinking of adding a second parameter to pass in a timeout time in seconds, but at this point i can't even get the function to work as is, so i am not worried about nicing it up yet...
trying to create a function to quickly test a URL
Moderator: General Moderators
Your solution lies in the CURL Library, I am really not a guru on this one... but it helped me...
There are 2 things you need to play with: CURLOPT_TIMEOUT, and the associative array at the end... there are lots of things you can do with it.
Here is the code i commonly use:
Hope this helps.
There are 2 things you need to play with: CURLOPT_TIMEOUT, and the associative array at the end... there are lots of things you can do with it.
Here is the code i commonly use:
Code: Select all
$ch = curl_init();
$target = "http://www.somewebsite.com/";
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $target);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); // Timeout in seconds
$exec = curl_exec( $ch );
if ( empty( $exec ) ) {
die( curl_error( $ch ) );
curl_close( $ch );
}
else {
$info = curl_getinfo( $ch ) ; // Not specifying an optional paramater will load an associative array with all the info you need
curl_close( $ch );
// Place any extra code you need to check for stuff in the associative array $info['total_time']... Check out http://www.php.net/manual/en/function.curl-getinfo.php for all the opstions.
}