views:

56

answers:

2

I'd like to study how headers are sent and received.

I know about PHP's header function and think I can just look at an actual request header (e.g. using Firebug) and make identical requests to a server (including spoofing the User-Agent). Is this correct?

The other problem is how do I get the header responses back? I want to analyze the response.

Thanks.

EDIT:

@Tatu, here's the code I ran:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://www.google.com/"); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; U; Linux x86_64; en-US) AppleWebKit/540.0 (KHTML, like Gecko) Ubuntu/10.10 Chrome/8.1.0.0 Safari/540.0');

$result = curl_exec($ch); curl_close($ch);

header('Content-type: text/plain'); echo($result);

+3  A: 
Tatu Ulmanen
Just for clarification, I can "spoof" User-Agent using cURL, right? I just want to see how servers respond to different user agents.
StackOverflowNewbie
@Tatu, I tried your code, but I didn't get headers (I got the actual file response, though). I'm editing my original post so I can show you my code.
StackOverflowNewbie
@StackOverflowNewbie, you need to have `CURLOPT_HEADER` set to 1 which I accidentally had as 0 in my example.
Tatu Ulmanen
@Tatu, I only want the headers. Is there a way to get just that?
StackOverflowNewbie
+1  A: 

If you want to send headers yourself, without using cURL, check out sockets in PHP.

http://php.net/sockets

nsr81
After you graduate from cURL - then sockets give you amazing power. But you can still do almost anything with cURL.
Xeoncross
yep, cURL provides better interface, but nothing beats raw sockets :-)
nsr81