Page 1 of 1

need advice geting the "location" from the header.

Posted: Fri Jun 01, 2007 4:52 pm
by pedrotuga
I am downloading pages with curl and i am want follow redirects manyaly because i need to correct the initial url in case i get a 301.

I though of two options.
one is to use regex ...

Code: Select all

preg_match("^(location:|location:))", $page);
another is to get the content of the fifth line as apparently the Location comes on that line.


I would prefer the second of course... but... does it allways comes on the fifth line?

Posted: Fri Jun 01, 2007 5:31 pm
by volka
pedrotuga wrote:I would prefer the second of course... but... does it allways comes on the fifth line?
No.
e.g.

Code: Select all

<?php
function foo($url) {
  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_HEADER, 1);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
  $s = curl_exec($ch);
  curl_close($ch);

  $s = explode("\n", $s);
  foreach($s as $n=>$h) {
    echo $h;
    if ( 0===strpos($h, 'Location:') ) {
      echo 'Line #', $n+1, "\n\n";
      break;
    }
  }
}

foo("http://perl.org");
foo("http://microsoft.com/ie");
foo("http://swiftmailer.sf.net");
?>
HTTP/1.1 302 Found
Date: Fri, 01 Jun 2007 22:31:24 GMT
Server: Apache/2.0.55 (Unix) mod_ssl/2.0.55 OpenSSL/0.9.7a
Location: http://www.perl.org/
Line #4

HTTP/1.1 301 Moved Permanently
Connection: close
Date: Fri, 01 Jun 2007 22:34:20 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
Location: http://www.microsoft.com/ie
Line #6

HTTP/1.1 302 Found
Date: Fri, 01 Jun 2007 22:31:25 GMT
Server: Apache/1.3.33 (Unix) PHP/4.3.10
Location: http://swiftmailer.sourceforge.net/
Line #4