How to use http_get_request_headers with PECL_HTTP version 2

If you’re using the version 2 pecl_http (possibly version 2.0.6?) on your webserver, perhaps Ubuntu or CentOS, with PHP5, maybe 5.3, 5.4, or 5.5, you may have noticed that after getting it all installed and adding this to your php.ini file:

extension=raphf.so
extension=propro.so
extension=http.so

But then when you try and use the function get_request_headers() you end up getting:

PHP Fatal error: Call to undefined function http_get_request_headers()

Well, when this extension switched to pecl/http v2 it changed a lot of things, and global functions was one of them. It now uses namespaces, and so instead of using http_get_request_headers() you’ll need to use something like this:

$headers = \http\Env::getRequestHeader();
print_r($headers);

The check out the docs for more details on how to use the new function, basically you’ll see

getRequestHeader: Retrieve one or all headers of the current HTTP request.

Parameters:

Optional string $header_name

The key of a header to retrieve.

Returns:

  • NULL, if $header_name was not found
  • string, the compound header when $header_name was found
  • array of all headers if $header_name was not specified

Related Posts:

  • No Related Posts
This entry was posted in Web Development and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *