Forum Moderators: coopster
<?php
function checkUrl($url) {
ini_set('default_socket_timeout', 7);
$a = file_get_contents($url,FALSE,NULL,0,20);
return ( ($a!= "") && ($http_response_header!= "") );
}
?>
Check that "default_socket_timeout" is in seconds and not milliseconds. I think it is, but I'm not sure.
TJ
[edited by: trillianjedi at 7:32 pm (utc) on July 3, 2007]
[jellyandcustard.com...]
If you're on PHP4, just use:-
$a = file_get_contents($url)
The downside is it will download the whole page, which is a waste of bandwidth if you're just checking if something is alive or dead.
You also need to check the manual for $http_response_header. Depending on your version of PHP it may not return an empty string for a 404. It might return full headers that you'll need to explode and check for a 404.
Added:-
Looking at your snippet I guess you're building a backlink checker, in which case you want to download the whole page anyway and RegEx for your domain inside <a href tags and ensure there isn't a no-follow or anything like that in there.
Warning: file_get_contents() [function.file-get-contents]: php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution in /home/#*$!/public_html/backlinks2.php on line 13
Warning: file_get_contents(http://example.com) [function.file-get-contents]: failed to open stream: Permission denied in /home/#*$!/public_html/backlinks2.php on line 13
Turbo
The timeout reflects to the time between 'open the site' and 'determine if the site is down', correct?
No. That function works at the TCP/IP socket layer, so will be based on a timeout further down the stack than HTTP.
Sounds to me like it's hanging waiting for an ACK. TCP/IP is blocking in nature, so your app is forced to sit and wait for a response (or a timeout from the OS) before it can do anything. The timeout will come from the OS, but the very nature of TCP/IP (and design of it) is such that it is allowed to be "down" for a period of time. So the PHP timeout may not happen until the underlying OS has timed-out and it sounds to me like it's getting around 9 seconds.
It might be a good idea to go multi-threaded, but that would mean coding rather than scripting (I think - I've written anything multi-threaded in PHP so don't even know if it's possible).