need advice geting the "location" from the header.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
pedrotuga
Forum Contributor
Posts: 249
Joined: Tue Dec 13, 2005 11:08 pm

need advice geting the "location" from the header.

Post 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?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
Post Reply