For Programmers: Free Programming Magazines  


Home > Archive > PHP Programming > October 2005 > Check if URL exists









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Check if URL exists
theboss3@gmail.com

2005-10-24, 3:55 am

(I know that's a horrible title; I couldn't think of anything better.)

Is there a simple way to check if a URL exists in PHP without using
Curl?

Ewoud Dronkert

2005-10-24, 3:55 am

theboss3@gmail.com wrote:
> Is there a simple way to check if a URL exists in PHP without using
> Curl?


Try http://php.net/fsockopen or http://php.net/fopen

--
E. Dronkert
theboss3@gmail.com

2005-10-24, 6:56 pm

Thanks! I wasn't sure if fsockopen would be good enough or not, but I
think it will be. I just need to catch the error.

That being said, is there a way to retrieve headers from another page,
like getheaders('http://example.com/') ?

Ewoud Dronkert

2005-10-24, 6:56 pm

theboss3@gmail.com wrote:
> That being said, is there a way to retrieve headers from another page,
> like getheaders('http://example.com/') ?


Open a socket, stop reading after the header, close socket.

--
E. Dronkert
theboss3@gmail.com

2005-10-24, 6:56 pm

Is this (http://simon.incutio.com/archive/20.../conditionalGet)
what you're talking about?

VS

2005-10-25, 6:56 pm

theboss3@gmail.com wrote:
> Thanks! I wasn't sure if fsockopen would be good enough or not, but I
> think it will be. I just need to catch the error.
>
> That being said, is there a way to retrieve headers from another page,
> like getheaders('http://example.com/') ?


I'm quite new to PHP, but I found and adpated this for getting the Time
Stamp of a URL, perhaps you can change it for what you need:

function filemtime_remote($uri)
{
$uri = parse_url($uri);
$uri['port'] = isset($uri['port']) ? $uri['port'] : 80;

// TimeOut
$tout = 10;
$handle = @fsockopen($uri['host'], $uri['port'], $errno, $errstr,
$tout);
if(!$handle)
return 0;

fputs($handle,"HEAD $uri[path] HTTP/1.1\r\nHost: $uri[host]\r\n\r\n");
$result = 0;
while(!feof($handle))
{
$line = fgets($handle,1024);
if(!trim($line))
break;

$col = strpos($line,':');
if($col !== false)
{
$header = trim(substr($line,0,$col));
$value = trim(substr($line,$col+1));
if(strtolower($header) == 'last-modified')
{
$result = strtotime($value);
break;
}
}
}
fclose($handle);
return $result;
}

--
VS
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com