Variable http_response_header is Deprecated in PHP 8.5

Variable http_response_header is Deprecated in PHP 8.5

PHP includes an HTTP wrapper that allows us to access files through HTTP. For example, the file_get_contents function can fetch remote files using HTTP as well as local files from the filesystem. Once a remote connection is successfully established, the HTTP wrapper fills the $http_response_header local variable with the returned HTTP response headers.

Since PHP 8.5, the $http_response_header variable is deprecated.

<?php

file_get_contents('https://httpbin.org/get');

var_dump($http_response_header);

Example will output:

Deprecated: The predefined locally scoped $http_response_header variable is deprecated, call http_get_last_response_headers() instead ...

To fix the deprecation, we can use the http_get_last_response_headers function, which returns the last HTTP response headers received through the HTTP wrapper. This serves as an alternative to the $http_response_header variable.

<?php

file_get_contents('https://httpbin.org/get');

var_dump(http_get_last_response_headers());

Leave a Comment

Cancel reply

Your email address will not be published.