preload preload


PHP fetch HTTP Request Headers – Performance Comparision

For many PHP scripts, it is always been a requirement to fetch the HTTP request headers coming from client for server side processing. I have made a list of all these functions and tried to benchmark them.

Here it follows:

Description

Following are the three functions which parse the HTTP request headers in PHP.

1. apache_request_headers : Built-in function. Will only work if you have compiled PHP as an Apache module.

function getHeaders() {
   return apache_request_headers();
}

2.  Custom function. Will work in all PHP installation. This function loops through $_SERVER global variable and extracts all entries which starts with HTTP_ and convert them to original key formats.

function getHeaders1() {
   $headers = array();
   foreach($_SERVER as $key => $val) {
      if (substr($key,0,5)=="HTTP_") {
         $key = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($val, 5)))));
         $headers[$key] = $val;
      }
   }
   return $headers;
}

3. Another custom function. Will work in all PHP installation. This function also loops through $_SERVER global variable. The difference than the previous one is, it extracts all the keys starting with HTTP_ first, and then loop only through them thus decreasing the complexity involved is looking into all the entries in $_SERVER.

function getHeaders2() {
   $headers = array();
   $keys = preg_grep('{^HTTP_}i', array_keys($_SERVER));
   foreach($keys as $val) {
      $key = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($val, 5)))));
      $headers[$key] = $_SERVER[$val];
   }
   return $headers;
}

Benchmark

The test involves running a loop of 1000 iterations over each function and recording the time taken by each one of them. The variation in the results might be only milliseconds and is of no concern on but that can result in a huge lag on a busy website.
Note: This test is ran over a system with 3G RAM and 2.5GHz processor. Results may differ. At the end of the post, is the attached script for the test. Feel free to use.

Here is the script:

<?php
/**
 * Performance Benchmark for functions
 * fetching HTTP request headers
 * Copyright (c) VoidWeb - http://voidweb.com
 */
$st = microtime(true);
for ($i = 0; $i < 1000; $i++) {
   getHeaders();
}
echo "apache_request_headers : \n";
echo $bigfirst = microtime(true) - $st;
echo "s<br/>";

$st = microtime(true);
for ($i = 0; $i < 1000; $i++) {
   getHeaders1();
}
echo "getHeaders1 : \n";
echo $bigfirst = microtime(true) - $st;
echo "s<br/>";

$st = microtime(true);
for ($i = 0; $i < 1000; $i++) {
   getHeaders2();
}
echo "getHeaders2 : \n";
echo $bigfirst = microtime(true) - $st;
echo "s<br/>";

function getHeaders() {
   return apache_request_headers();
}

function getHeaders1() {
   $headers = array();
   foreach($_SERVER as $key => $val) {
      if (substr($key,0,5)=="HTTP_") {
         $key = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($val, 5)))));
         $headers[$key] = $val;
      }
   }
   return $headers;
}

function getHeaders2() {
   $headers = array();
   $keys = preg_grep('{^HTTP_}i', array_keys($_SERVER));
   foreach($keys as $val) {
      $key = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($val, 5)))));
      $headers[$key] = $_SERVER[$val];
   }
   return $headers;
}
?>

Here are the results:
apache_request_headers : 0.030539035797119s
getHeaders1 : 0.7525520324707s
getHeaders2 : 0.51612019538879s

As we can see that the builtin apache_request_headers function is having the minimum time for execution. second is getHeaders2 and then come getHeaders1. So if you have a setup which is using mod_php apache module, it is recommended to use builtin apache_request_headers function whereas for other setup like PHP as a CGI module,  getHeaders2 can be used ideally.

Please share if there can be any other way of fetching request headers in PHP. Thanks.

Liked the content? Then why not share with your pals

  • Leave a Reply

    * Required
    ** Your Email is never shared