如何在没有PECL的情况下获得http_parse_headers的功能?

Shr*_*ath 12 php http

我在Windows上使用PHP 5.3.5,我找不到任何pecl_http.dll适用于我的安装.

所以我的问题是,

如何在http_parse_headers不使用PECL 的情况下获得功能?

Ber*_*rak 16

从文档页面,第一条评论:

 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;
     }
}
Run Code Online (Sandbox Code Playgroud)

或者,您可能想要阅读如何在Windows上安装PECL扩展,说实话,我对此一无所知.


Gor*_*don 9

您可以在以下位置获取Windows的扩展程序

这是其中一个php_http-5.3-*-x86.zip文件.检查你安装的PHP并选择正确的PHP,例如我的PHP是php-5.3.6 -nts-Win32-VC9 -x86,所以我需要php_http-5.3 -nts-svn20091125-vc9 -x86.zip.

下载zip并将php_http.dll解压缩到您的扩展文件夹.扩展文件夹应该是php安装目录中的/ ext文件夹.如果您不确定,请打开您的php.ini文件并搜索以下行:

; Directory in which the loadable extensions (modules) reside.
; http://php.net/extension-dir
; extension_dir = "./"
; On windows:
extension_dir = .\ext
Run Code Online (Sandbox Code Playgroud)

extension_dir的值是您必须放置dll的位置.如果您不确定php.ini的位置,请打开命令提示符并执行操作

php --ini
Run Code Online (Sandbox Code Playgroud)

这将告诉你php.ini的位置.它会输出类似的东西

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)
Run Code Online (Sandbox Code Playgroud)

复制dll后,将扩展名添加到php.ini中以启用它.找到它所说的内容

;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;
Run Code Online (Sandbox Code Playgroud)

应该有多条与此类似的行:

;extension=php_sybase_ct.dll
extension=php_tidy.dll
;extension=php_xmlrpc.dll
extension=php_xsl.dll
;extension=php_zip.dll
Run Code Online (Sandbox Code Playgroud)

添加以下行:

extension=php_http.dll
Run Code Online (Sandbox Code Playgroud)

保存php.ini并在命令提示符下键入以下内容:

php --ri http
Run Code Online (Sandbox Code Playgroud)

你现在应该从一开始就获得相当广泛的输出

http
HTTP Support => enabled
Extension Version => 1.7.0-dev
… more stuff
Run Code Online (Sandbox Code Playgroud)

这意味着,您已成功安装扩展,现在可以使用它.

请注意,为了能够在Windows上加载此扩展,您还需要加载以下PHP扩展:hash,iconv和SPL.


Red*_*Taz 9

这也是从http_parse_headersPHP文档中解除的.当我将它与@Berry Langerak的答案(使用microtime)进行比较时,我发现使用10个标题的样本(大概是因为它不使用正则表达式)平均快17%.

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

注意:这包括作者在稍后的评论中指出的小错误的修复.


正则表达式功能

0.00035881996
0.00036096572
0.00034999847
0.00043797492
0.00033497810

平均值:0.000368547434

这个功能

0.00006198883
0.00006604194
0.00007104873
0.00006413459
0.00006389617

平均0.000065422052