Getting all headers w/php

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
inosent1
Forum Commoner
Posts: 97
Joined: Wed Jan 28, 2009 12:18 pm

Getting all headers w/php

Post by inosent1 »

Hi

when i use livehttpheaders i get the following

Code: Select all


// PART 1
http://www.thesite.com/the_page.html

GET /the_page.html HTTP/1.1
Host: http://www.thesite.com
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/17.0 Firefox/17.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Connection: keep-alive
Cookie: __cfduid=dbe50c0c69cc696be75f57edfafa10fd81375403991; __gads=ID=1a02ee2637fc796b:T=1375403992:S=ALNI_Mbo-WSQLc3M2i2MshXDtgHi2OgDAw; __utma=130432987.745463861.1375404158.1375404158.1375406294.2; __utmc=130432987; __utmz=130432987.1375404161.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); vnd=; qsgr=1; qidxr=1; QUADIDX=3; qsg=13158060

// PART 2


HTTP/1.1 301 Moved Permanently
Server: cloudflare-nginx
Date: Fri, 02 Aug 2013 02:23:54 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
Location: /some_other_folder/123456
Set-Cookie: vnd=; path=/
CF-RAY: 9774f0a91b90077


but all the code i have tried using php to get the header only gives me [PART 1] above, but i need [PART 2] because in that header, where you see Location there is a trailing number after where it says "some_other_folder/"

although i have tried countless exotic functions, using every imaginable device, they all output the same thing, they never move past PART 1 (as i call it) to PART 2, or the next header in the 'header chain' so to speak.

so no matter the code, they all end up with the same thing as this code gives me:

Code: Select all

<?
$url='http://whatever_site_you_want.com';

print_r(get_headers($url));

print_r(get_headers($url, 1));


any ideas appreciated!

TIA
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Getting all headers w/php

Post by AbraCadaver »

mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Getting all headers w/php

Post by requinix »

cURL can do it too with a combination of CURLOPTions, exactly what escaping me at the moment - like maybe CURLINFO_HEADER_OUT and CURLOPT_FOLLOWLOCATION? I bet the built-in streams and stream contexts can too.

Absolute worst case that I'm sure you wouldn't have to fall back to is handling the redirections yourself: loop until you get a non-3xx response, and while you do grab the Location: header, figure out the new URL (as Location: often does not have a fully-qualified URL), and fetch that.
Post Reply