0

Possible Duplicate:
How to get the real URL after file_get_contents if redirection happens?

I'm using file_get_contents('http://someurl.com/1893849') to get contents of a url, but the url gets redirected. For example /1893849 ends up at /some-url-path. Is there a way, in addition to getting the file contents, to also know the end path?

Community
  • 1
  • 1
sameold
  • 18,400
  • 21
  • 63
  • 87

1 Answers1

1

Use curl example :

curl_setopt($init, CURLOPT_FOLLOWLOCATION, 1);

if you need know adres of redirec

curl_setopt($init, CURLOPT_HEADER, 1);
curl_setopt($init, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($init, CURLOPT_HTTPHEADER, array('Expect:') );

if ( strstr($content, 'Moved Temporarily') or  
strstr($content, 'Moved Permanently') or strstr($content, '302 Found')   ) :

if ( preg_match('@Location: (.*?)\n@', $content, $red) ) :

print_r($red);

endif; 
user956584
  • 5,316
  • 3
  • 40
  • 50
  • It probably isn't a good idea to look for the English strings `Moved Temporarily` or `Moved Permanently` or `302 Found`. Checking the very first line of output for the status code (and only the status code) would be far more robust. – sarnold Oct 16 '11 at 07:10