r/programmingrequests • • Apr 21 '17

HTTP Header Request Help

Hello,

I'm somewhat new to all of this, so apologies if this is ignorant, but I can't find the answer to my question anywhere. I have been trying desperately to change the HTTP Request headers of my php request using Guzzle and curl.

I've had no luck at all. I've tried many, many methods. Some of which I feel should have worked. I'm starting to think there's a piece of this puzzle that I just don't know. What I think is happening is this:

  1. I'm outputting the results of getallheaders() -PHP- into the browser to see what my request headers are. They always come back the same thing.

  2. I've tried using getallheaders() to output to the console, but it doesn't work. I think because it's an extension of apache, it will only work in the browser?

  3. I also think the reason I can't change the request header is because Chrome is setting their own request header and if I were to do this solely through the console, I wouldn't run into this issue. Is this correct?

2 Upvotes

5 comments sorted by

2

u/NoG5 Apr 22 '17

something like this?

getallheaders.php

<?php
    echo "<pre>";
    print_r(getallheaders());
    echo "</pre>";
?>

curl_headers.php

<?php
$ch = curl_init("http://nog5.com/php/getallheaders.php");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'My-http-header: some value',
    'Some-other-header: 100,200'
    ));
echo curl_exec($ch);
curl_close($ch);
?>

1

u/FlandersFlannigan Apr 22 '17

This is where I get so confused. I tried the first code (getallheaders()) and output it in the browser and the user-agent shows my actual settings - even though I have included my own headers, including user-agent. However, when I make the request in the console (via guzzle) the debugger shows the http request with the headers that I set up. Same code.

What am I missing? What is the server actually seeing? I plan to do some web scraping so I I'm trying to understand what the servers will actually be seeing.

2

u/NoG5 Apr 22 '17

its seeing what getallheaders() is returning, if you make a request to the getallheaders.php directly in a browser it will show normal browser headers and if you program your own get request to it as in curl_headers.php it will show the headers that was added in the request

this is what i see when going to getallheaders.php directly with firefox

[Host] => nog5.com
[User-Agent] => Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:52.0) Gecko/20100101 Firefox/52.0
[Accept] => text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
[Accept-Language] => en-GB,en;q=0.5
[Accept-Encoding] => gzip, deflate
[Referer] => https://www.reddit.com/r/programmingrequests/comments/66punl/http_header_request_help/
[Connection] => keep-alive
[Upgrade-Insecure-Requests] => 1

and when going to getallheaders.php page with curl only these headers, but it is still the same page and it still just runs getallheaders()

[Host] => nog5.com
[Accept] => */*
[My-http-header] => some value
[Some-other-header] => 100,200

1

u/FlandersFlannigan Apr 22 '17

That's what I thought was happening. Thank you for taking the time to explain that. It was very helpful. One thing I'm curious about is how you're getting getallheaders() to run outside of the browser? I try running my script.php in the console and I always get "Fatal error: Call to undefined function getallheaders() "

1

u/NoG5 Apr 22 '17

getallheaders() is running on the server side as its a php function, not client side like javascript