Call to Undefined Function Apache_Request_Headers()

Call to undefined function apache_request_headers()

From the docs, before the release of PHP 5.4.0:

This function is only supported when PHP is installed as an Apache module.

PHP 5.4.0 and later support this function unconditionally.

Said docs also include replacement functions that mimic the functionality of apache_request_headers by stepping through $_SERVER.

call to undefined function apache_request_headers Slim framework

Updating, this seems to take care of the the header problem

   if (!function_exists('apache_request_headers')) { 

function apache_request_headers() {
foreach($_SERVER as $key=>$value) {
if (substr($key,0,5)=="HTTP_") {
$key=str_replace(" ","-",ucwords(strtolower(str_replace("_"," ",substr($key,5)))));
$out[$key]=$value;
}else{
$out[$key]=$value;
}
}
return $out;
}
}

Is there a apache_request_headers alternatve for displaying HTTP Headers

It sounds like your local server is running Apache and your remote server is not, as this function only works with Apache (unless the server is running PHP 5.4.0, then it also works under FastCGI.

On the PHP Manual page for this function, one of the commenters included a replacement function that will be declared only if the built-in one doesn't exist. I haven't tested this, but I've seen the same function posted elsewhere.

if( !function_exists('apache_request_headers') ) {
function apache_request_headers() {
$arh = array();
$rx_http = '/\AHTTP_/';

foreach($_SERVER as $key => $val) {
if( preg_match($rx_http, $key) ) {
$arh_key = preg_replace($rx_http, '', $key);
$rx_matches = array();
// do some nasty string manipulations to restore the original letter case
// this should work in most cases
$rx_matches = explode('_', $arh_key);

if( count($rx_matches) > 0 and strlen($arh_key) > 2 ) {
foreach($rx_matches as $ak_key => $ak_val) {
$rx_matches[$ak_key] = ucfirst($ak_val);
}

$arh_key = implode('-', $rx_matches);
}

$arh[$arh_key] = $val;
}
}

return( $arh );
}
}

PHPUnit - getallheaders not work

From this article:

If you use Nginx, PHP-FPM or any other FastCGI method of running PHP
you’ve probably noticed that the function getallheaders() does not
exist. There are many creative workarounds in the wild, but PHP offers
two very nice features to ease your pain.

From user contributed comments at getallheaders() function on PHP manual by joyview at gmail dot com

if (!function_exists('getallheaders')) {
function getallheaders() {
$headers = [];
foreach ($_SERVER as $name => $value) {
if (substr($name, 0, 5) == 'HTTP_') {
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
}
}
return $headers;
}
}

PHP getting headers sent with curl

Fetches all HTTP request headers from the current request with

apache_request_headers();

look at php.net apache-request-headers

if apache_request_headers() is not available, loop trough the superglobal $_SERVER and get info you need, the request headers starting with HTTP_, like

$headers = array();

foreach ($_SERVER as $key => $value) {
if (preg_match('/^HTTP_.+/', $key)) {
// sanitize keys removing HTTP_
$headers[str_replace('HTTP_','',$key)] = $value;
}
}

to use

echo $headers['API-Key'];


Related Topics



Leave a reply



Submit