dpblwmh5218 2017-04-11 07:54
浏览 98
已采纳

\ http_parse_headers()有反向功能吗?

PHP function \http_parse_headers() parses HTTP headers into an associative array.

But is there some reverse function? Which parses associative array into HTTP headers?

Cannot find anything :(

(it's for saveing email into textual .eml file)

  • 写回答

1条回答 默认 最新

  • doutang7383 2017-04-11 09:33
    关注

    There isn't a function that turns associative array into text-representation of headers. The reason: this function is extremely trivial to create.

    Headers are defined as key: value delimiter is . There is another delimiter between headers and body.

    Lets take an example array:

    $headers = [
        'Content-Length': 50,
        'Content-Encoding': 'gzip'
    ];
    

    The goal: provide a string that represents HTTP headers

    function parse_array_to_headers(array $headers)
    {
        $result = [];
        $delimiter = "
    ";
    
        foreach($headers as $name => $value)
        {
            $result[] = sprintf("%s: %s", $name, $value);
        }
    
        return implode($delimiter, $result);
    }
    

    Note: this function will not check the validity of array and it won't return the string with two repetitions of at the end. This example serves to show how easy it should be to add missing function. Adjust according to your needs. Also, I didn't test this so don't copy paste it! :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?