PHP get_headers function always HTTP/1.1

346 views Asked by At

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!

1

There are 1 answers

1
magrigry On BEST ANSWER

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.