Re: HOW TO: check if a URL exists or not - ErROR 404 ?
gsb <gsb@qwest.ne t> wrote:[color=blue]
> My php passes through some links. But I want to redirect if the URL does not
> exist.
> How best to check if a URL is there or not?[/color]
Do a HEAD request to the URL using a socket, if you get a 200 it's
there. If you get something else it might not be (maybe it's a redirect
(301 IIRC)).
Re: HOW TO: check if a URL exists or not - ErROR 404 ?
Ah, sorry.
I appreciate your response but it is a bit over me.
I've used sockets only in an example that I modified, and do not know how to
do a 'HEAD request.'
I can not find a reasonable example in the ref. I use.
Could you elaborate a little, or show an example?
If not, thanks anyway.
gsb
"Daniel Tryba" <news_comp.lang .php@canopus.nl > wrote in message
news:bncfh3$3s$ 2@news.tue.nl.. .[color=blue]
> gsb <gsb@qwest.ne t> wrote:[color=green]
> > My php passes through some links. But I want to redirect if the URL does[/color][/color]
not[color=blue][color=green]
> > exist.
> > How best to check if a URL is there or not?[/color]
>
> Do a HEAD request to the URL using a socket, if you get a 200 it's
> there. If you get something else it might not be (maybe it's a redirect
> (301 IIRC)).
>
> --
>
> Daniel Tryba
> r[/color]
Re: HOW TO: check if a URL exists or not - ErROR 404 ?
gsb <gsb@qwest.ne t> wrote:
[use HEAD to check for URL validity][color=blue]
> Ah, sorry.
> I appreciate your response but it is a bit over me.
> I've used sockets only in an example that I modified, and do not know how to
> do a 'HEAD request.'
> I can not find a reasonable example in the ref. I use.[/color]
It's quite simple, if the url you want to check is
This will echo 'HTTP/1.0 200 OK' if the url exists and might return
'HTTP/1.0 404 Requested URL not found' for a non existant page among
other headers.
You can disect an url with parse_url to get the hostname/port/path from
a URL. You might want to do HTTP/1.1 request (where a Host: header is
required (read RFC 2616 if you want to know more)).
But maybe you should take a look at PEAR, there appears to be an
HTTP::HTTP_Clie nt class that prbably does something like this...
Re: HOW TO: check if a URL exists or not - ErROR 404 ?
Daniel Tryba <news_comp.lang .php@canopus.nl > writes:
[color=blue]
> gsb <gsb@qwest.ne t> wrote:
> [use HEAD to check for URL validity][color=green]
> > Ah, sorry.
> > I appreciate your response but it is a bit over me.
> > I've used sockets only in an example that I modified, and do not know how to
> > do a 'HEAD request.'
> > I can not find a reasonable example in the ref. I use.[/color]
>
> It's quite simple, if the url you want to check is
> http://hostname.tld/foo:
>
> <?php
> if($sock=fsocko pen('hostname.t ld',80))
> {
> fputs($sock, "HEAD /foo HTTP/1.0\r\n\r\n");
>
> while(!feof($so ck))
> {
> echo fgets($sock);
> }
> }
> ?>
>
> This will echo 'HTTP/1.0 200 OK' if the url exists and might return
> 'HTTP/1.0 404 Requested URL not found' for a non existant page among
> other headers.[/color]
What about (e.g.), 300 "Moved Permanently", 301 "Found", 307
"Temporary Redirect", ... ?
[color=blue]
> You can disect an url with parse_url to get the hostname/port/path from
> a URL. You might want to do HTTP/1.1 request (where a Host: header is
> required (read RFC 2616 if you want to know more)).[/color]
"Host" should definitely be provided, as virtual hosting is quite
common these days.
[color=blue]
> But maybe you should take a look at PEAR, there appears to be an
> HTTP::HTTP_Clie nt class that prbably does something like this...[/color]
I believe you can use the curl_* functions as well...
Comment