Re: emulate a browser

From: Date: Thu, 24 Jan 2002 09:33:24 +0000
Subject: Re: emulate a browser
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-81844@lists.php.net to get a copy of this message
It works really nice, Michael, thanks again. I have one more question, maybe you can answer me : If the domain name has been redirected with hte permanent redirect, I get the 301 Moved Permanently header response. But if the domain name has been frame-forwarded, I have no header response. Is it normal ? Do you know if there is some alternatives so I could check if the domain name is used, even on a frame-forward type redirectection ? Moreover, if the domain has been redirected, is it possible to get the url it has been redirected to ? Thanks again for your help Frank ----- Original Message ----- From: "Michael Sims" <mhsims@midsouth.rr.com> To: "Frank Benady" <f.benady@french-connexion.fr>; <php-general@lists.php.net> Sent: Thursday, January 24, 2002 2:28 AM Subject: Re: [PHP] emulate a browser > At 01:45 AM 1/24/2002 +0100, Frank Benady wrote: > >Hi All > > > >Can I emulate the first request of a browser when it tries to connect to a > >distant server using a domain name and parse the answer from the remote > >server to know if there is a website located there or if there is no hosting > >(or even some kind of forwarding) configured for this domain name ? > >Is ther some http functions in PHP which permit to do this test ? > > You can use fsockopen() to open a socket connection to a remote host via > port 80 (HTTP). If fsockopen() returns a valid file pointer then that > means there is something listening on port 80 on the host (99% it will be a > web server). > > You could then use fputs() to send a request to the server, such as: > > "HEAD / HTTP/1.0\r\n\r\n" > > Normally browsers use GET instead of HEAD, but in your case you are only > interested in the servers response (the HTTP headers) and not the actual > file/page itself. > > You could then use fgets() to read the response sent back from the server > and parse it to get the information you wanted (for example, look for a > "Location: ..." line in the headers to see if the page is trying to > redirect the browser). > > I have a function that checks to see if a particular file is available via > HTTP from a remote host. It takes a full URL and returns true if the > page/file exists, and false if it doesn't. With some work you could modify > this script to achieve what you want. I'm posting it below. Ask if you > have any questions about it. > > Oh, BTW, I'm sure that I have (as usually) went totally overboard with this > function and someone will now probably point out that PHP has something > built in to do what I'm doing here...but I couldn't find it and I had fun > writing this function anyway. :-) > > <? > function http_file_exists ($url) { > > if > (!preg_match("/^http:\/\/([^\/]+)\/.*$/i",$url,$matches)) > { > > return "Error - incorrect format"; > > } else { > > $host = $matches[1]; > $fp = fsockopen ($host, 80, $errno, $errstr, 30); > if (!$fp) return "Error - couldn't connect to host"; > else { > fputs ($fp, "HEAD $url HTTP/1.0\r\n\r\n"); > $response = fgets ($fp,128); > fclose ($fp); > return (eregi("^.+200 OK.+$",$response) ? true : false); > } > > } > } > ?> > >

« previous php.general (#81844) next »