I am using PHP 5.3.5 on windows, and I didn't find any pecl_http.dll
that works with my installation.
So my question is,
How do I get the functionality of http_parse_headers
without using PECL?
You can get the extension for Windows at
It's one of the php_http-5.3-*-x86.zip
files. Check which PHP you have installed and pick the right one, e.g. my PHP is a php-5.3.6-nts-Win32-VC9-x86, so I needed php_http-5.3-nts-svn20091125-vc9-x86.zip.
Download the zip and extract the php_http.dll to your extension folder. The extension folder should be the /ext folder in your php installation directory. If you are not sure, open your php.ini file and search for these lines:
; Directory in which the loadable extensions (modules) reside.
; http://php.net/extension-dir
; extension_dir = "./"
; On windows:
extension_dir = .\ext
The value for extension_dir is where you have to place the dll. If you are unsure where your php.ini is located, open a command prompt and do
php --ini
This will tell you where your php.ini is. It will output something like
Configuration File (php.ini) Path: C:\Windows
Loaded Configuration File: C:\php5\php-5.3.6-nts-Win32-VC9-x86\php.ini
Scan for additional .ini files in: (none)
Additional .ini files parsed: (none)
After you have copied the dll, add the extension to your php.ini to enable it. Find where it says something like
;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;
There should be multiple lines similar to this:
;extension=php_sybase_ct.dll
extension=php_tidy.dll
;extension=php_xmlrpc.dll
extension=php_xsl.dll
;extension=php_zip.dll
Add the following line:
extension=php_http.dll
Save the php.ini and type the following at your command prompt:
php --ri http
You should now get a rather extensive output starting with
http
HTTP Support => enabled
Extension Version => 1.7.0-dev
… more stuff
This means, you have successfully installed the extension and can use it now.
Note, that in order to be able to load this extension on Windows, you additionally need to load the following PHP extensions: hash, iconv and SPL.
This is also lifted from the PHP Documentation for http_parse_headers. When I compared it with @Berry Langerak's answer (using microtime
) I found it to be 17% faster on average, using a sample of 10 headers (presumably because it doesn't use regular expressions).
if (!function_exists('http_parse_headers')) {
function http_parse_headers($raw_headers) {
$headers = array();
$key = '';
foreach(explode("\n", $raw_headers) as $i => $h) {
$h = explode(':', $h, 2);
if (isset($h[1])) {
if (!isset($headers[$h[0]]))
$headers[$h[0]] = trim($h[1]);
elseif (is_array($headers[$h[0]])) {
$headers[$h[0]] = array_merge($headers[$h[0]], array(trim($h[1])));
}
else {
$headers[$h[0]] = array_merge(array($headers[$h[0]]), array(trim($h[1])));
}
$key = $h[0];
}
else {
if (substr($h[0], 0, 1) == "\t")
$headers[$key] .= "\r\n\t".trim($h[0]);
elseif (!$key)
$headers[0] = trim($h[0]);
}
}
return $headers;
}
}
Note: this includes the fix of the small mistake the author points out in a later comment.
Regex Function
0.00035881996
0.00036096572
0.00034999847
0.00043797492
0.00033497810Average: 0.000368547434
This Function
0.00006198883
0.00006604194
0.00007104873
0.00006413459
0.00006389617Average 0.000065422052
From the documentation page, first comment:
if( !function_exists( 'http_parse_headers' ) ) {
function http_parse_headers( $header )
{
$retVal = array();
$fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $header));
foreach( $fields as $field ) {
if( preg_match('/([^:]+): (.+)/m', $field, $match) ) {
$match[1] = preg_replace('/(?<=^|[\x09\x20\x2D])./e', 'strtoupper("\0")', strtolower(trim($match[1])));
if( isset($retVal[$match[1]]) ) {
$retVal[$match[1]] = array($retVal[$match[1]], $match[2]);
} else {
$retVal[$match[1]] = trim($match[2]);
}
}
}
return $retVal;
}
}
Alternatively, you may want to read how to install a PECL extension on Windows, to be honest, I don't know anything about that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With