tags:

views:

191

answers:

5

Hi all.

Let's say I have a server at www.myhost.com. From there by using php I want to retrieve the html document that the php file www.anotherhost.com/somefile.php produces. I also want to use php to do this. anotherhost is not on the same server as myhost.

I know that functions such as fopen, f_get_contents and file can do this by simply executing e.g.

$fp = fopen ("http://www.anotherhost.com/somefile.php")

and from there fp can be used as any other file pointer to read the contents.

BUT, for some reason I also want to know if somefile.php at anotherhost ordered a client-side redirect when I tried to retrieve somefile.php´s resulting html document. somefile.php can do this by

header ("Location: http://www.anotherhost.com/extrafile.php")

Now fopen will retrieve the html document that extrafile.php produces, without detecting that a client-side redirect has been performed.

Is there some functionality in PHP that enables you to retrieve html documents from other servers AND notifies you if a redirect has taken place? It is acceptable if I must follow the redirect by myself (not done automatically), as long as I'm told what the new URL is.

Executing arbitrary commands with the function system is not preferred.

PS. If you are going to suggest fsockopen, then please explain why i get the error "Unable to find the socket transport "http" - did you forget to enable it when you configured PHP?" when I try to execute

fsockopen ("http://localhost")

and I also get "Failed to parse address "localhost" when I do

fsockopen ("localhost")

Thanks for reading. Help would be greatly appreciated.

A: 

You probably want a full HTTP client, not stream wrappers and fopen()/file_get_contents(). I think its possible to do what you want with stream wrappers using stream_get_meta_data() but you would be better off with a fuller implementation.

mst
+1  A: 

You can use the Zend_Http_Client from Zend Framework. It has methods for retrieving the number of redirects as well as setting a limit to the number of redirects to follow etc. If you just want to know how it's done, then you can look into the source code and try to figure it out. Shouldn't be too hard I think.

PatrikAkerstrand
A: 

If you don't want to go down the Zend or stream route, you could always use the cURL functions. These will let you return the headers, hence you could analyse these to ascertain whether any re-directs are being issued.

(You can also use the 'CURLOPT_MAXREDIRS' option to specify that it shouldn't follow re-directs.)

middaparka
+1  A: 

I would use cURL for this. If the redirect is specified in the header, instead of apache mod_rewrite for example, then you should be able to detect if a redirect is present by grabbing all of the headers sent by the response.

Optionally, there is a setting to automatically follow the redirect (http://us.php.net/manual/en/function.curl-setopt.php).

kdevine
A: 

As of php 5.2 you can use the stream api and a stream_notification_callback

e.g.

function notification($code, $severity, $message, $mcode, $transferred, $max) {
  if ( STREAM_NOTIFY_REDIRECTED===$code ) {
    echo 'redirected to ', $message, "\n";
  }
}
$ctx = stream_context_create(null, array('notification' => 'notification'));
$c = file_get_contents("http://spiegel.de", false, $ctx);
echo 'len=', strlen($c);

prints

redirected to http://www.spiegel.de/
len=144446
VolkerK