0

The PHP get_headers function returns an array, eventually including a string like HTTP/1.1 200 OK. The URLs I pass to the function are https. For some reason the successful response of get_headers always has HTTP/1.1, but Firefox devtools - network tab says it's HTTP/2.

I call the function like $headers = get_headers("https://SOME_URL", 1);. The second parameter just affects the format of the response array. Then handle it like if($headers[0] == 'HTTP/1.1 200 OK') { ... }

Unfortunately I can't provide a better snippet to my problem. Does get_headers do something under the hood to always show the response as HTTP/1.1 ? Did someone else run into this ?

I searched various posts and read some of the docs but I could not find anything related to my question.

Any help is appreciated!

  • My guess is that at the time it leaves PHP, it _is_ an HTTP/1.1 request, and something between that and your browser is converting it to HTTP/2. Do you have any CDN or other proxy in front of your web server? What web server are you using? What version of PHP, and how is it running (Apache module, FPM, etc)? – IMSoP Oct 12 '20 at 09:02

1 Answers1

2

If the traffic is not proxied, I suppose get_headers just not support HTTP/2 (I suppose that all native PHP functions does not support HTTP/2, maybe depending on the PHP version you use).

Maybe try an other HTTP client like Guzzle and force the HTTP version like this :

use GuzzleHttp\Client;

$client = new Client();
$client->get('http://test.test', [
    'version' => 2.0,
    'debug' => true,
]);

If the server still return a response with HTTP version 1.1, then your traffic is probably proxied. If not, get_headers probably does not support HTTP/2.

Edit : According to this post Make HTTP/2 request with PHP that is not so old, PHP only natively support HTTP/2 with cURL.

magrigry
  • 405
  • 2
  • 8