Checking is a remote page exists

I’m writing a script where I need to check if a url actually works. I tried something like

if(@file_get_contents($url))
url works

However, for some reason when url does not work it times out.

Is there a better and faster way to do this? I’m pretty new to php so please keep the words simple :)

had a simular prob a while ago.
http://phphelp.com/forums/viewtopic.php?p=29222#29222

to get the timout handled u can use stream_set_blocking($fp,0) and check the strlen() of the value returned by fgetc()/fgets().

sounds compicated but isn’t.

  1. set a filepointer to ur url: $fp=fopen($url)
  2. call stream_set_blocking($fp,1); the second parameter is the timeout in seconds (how long ur php-script should try to read the url befor giving up)
  3. get the content (one char should be enough): $content=fgetc($fp);
  4. and then just check if there was content returned if(strlen($content))

that means:
[php]$fp=fopen($url);
stream_set_blocking($fp,1);
$content=fgetc($fp);
if(strlen($content))
{
/* url works*/
}[/php]

but that don’t tell u more about the url than that there is content returned.

detaild desc of the functions:
http://php.net/fopen
http://php.net/stream_set_blocking
http://php.net/fgetc
http://php.net/strlen

Sponsor our Newsletter | Privacy Policy | Terms of Service