简体   繁体   English

PHP和HTTP标头换行符:用来表示什么字符?

[英]PHP and HTTP Header Line Breaks: What character used to represent?

I'm looping through each line of a series of CURL returned http headers, trying to detect when one ends and the next begins. 我遍历了一系列CURL返回的HTTP标头的每一行,试图检测何时结束以及下一个开始。 I know that an http header terminates with an empty line, but what character is used to represent this line break in php? 我知道http标头以空行结尾,但是用什么字符来表示php中的此换行符? I've tried with \\n but it doesn't seem to work. 我已经尝试过\\n但是似乎没有用。 I certainly could be doing something wrong. 我当然可能做错了。

What character is used to represent the line break used to terminate a header? 用什么字符表示用于终止标题的换行符?

Here's my existing code: 这是我现有的代码:

$redirect = '';
$regs = '';
foreach ($curl_response as $line)
{   
    if ($line != "\n")
    {   # line is not a linebreak, so we're still processing a header block

        if (preg_match("(HTTP/[0-9]\.[0-9] [0-9]{3} .*)",$line))
        {   # line is the status code
            # highlight the outputted line
            $output .= "<b style='background: yellow;'>$line</b>";
        }

        elseif (preg_match("/^Location: (.*)$/m",$line,$regs)) 
        {   # the line is a location header, so grab the location being redirected to
            # highlight the outputted line
            $output .= "<b style='background: purple; color: white;'>$line</b>";
            $redirect = $regs[1];
        }

        else 
        {   # some other header, record to output
            $output .= $line;
        }

    }

    else 
    {   # we've reached a line break, so we're getting to a new block of redirects
        $output .= "\nreached line break\n";
        if ($redirect != '')
        {   # if we recorded a redirect above, append it to output
            $output .= "\n\nRedirecting to $redirect\n\n";
            $redirect = '';
        }

    }   

}

echo $output;

Solved - Turns out that \\r is what I should have been matching on. 解决 -原来\\r是我应该匹配的对象。 Very odd. 很奇怪。 Not sure if this changes per site, or if it's something set in curl. 不确定是否每个站点都会更改,或者是否设置了卷曲。 So far its \\r on all sites I've tried. 到目前为止,其\\r上的所有网站我已经试过。

Edit 2 : Doh. 编辑2 :Doh。 I think it's because in order to get the header into an array of lines, I exploded it on \\n . 我认为这是因为为了使标头成一行行,我在\\n上进行了分解。 So perhaps any \\r\\n are now just \\r ... 所以也许任何\\r\\n现在只是\\r ...

$c = explode("\n",$content);

You need to also check for "\\r\\n" and "\\r", as those are also valid terminating empty lines. 您还需要检查“ \\ r \\ n”和“ \\ r”,因为它们也是有效的终止空行。

When in canonical form, media subtypes of the "text" type use CRLF as the text line break. 当采用规范形式时,“文本”类型的媒体子类型使用CRLF作为文本换行符。 HTTP relaxes this requirement and allows the transport of text media with plain CR or LF alone representing a line break when it is done consistently for an entire entity-body. HTTP放宽了此要求,并允许在整个实体主体上一致执行时仅使用纯CR或LF表示换行符的文本媒体传输。 HTTP applications MUST accept CRLF, bare CR, and bare LF as being representative of a line break in text media received via HTTP. HTTP应用程序必须接受CRLF,裸CR和裸LF来表示通过HTTP接收的文本媒体中的换行符。

-- HTTP/1.1: Protocol Parameters - 3.7.1 Canonicalization and Text Defaults -HTTP / 1.1:协议参数-3.7.1规范化和文本默认值

The headers terminate with a double line break with no space in between (ie an empty line). 标头以双换行符结尾,中间没有空格(即,空行)。 A line break can be either "\\n", "\\r\\n" or just "\\r". 换行符可以是“ \\ n”,“ \\ r \\ n”或只是“ \\ r”。 Even though the latter is uncommon it still needs to be accounted for. 即使后者不常见,仍需要考虑。

Perhaps you could find the end of the headers with a regular expression like 也许您可以找到带有正则表达式的标头结尾,例如

list($headers) = preg_split('/(\r\n?|\n)(\r\n?|\n)/', $httpresponse);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM