PHP和HTTP头断行:什么性格中使用以代表什么?
-
12-09-2019 - |
题
我的循环,通过每个行一系列的卷曲返回http头,试图检测,当一个结束和下一个开始。我知道,http头终止与一个空行,但什么样的角色是用于表示这条线断php?我已经尝试 \n
但它似乎不到工作。我肯定可以做的事情是错误的。
什么样的角色是用于表示行使用终止的头?
这是我的现有编码:
$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;
解决了 -原来那个 \r
为什么我应该已经匹配。非常奇怪。不知道,如果这一变化每个网站,或者如果它是落在卷曲。迄今为止它的 \r
所有网站上,我已经试过了。
编辑2:卫生署。我认为这是因为为了得到头到一阵线,我分解它 \n
.因此,或许任何 \r\n
现在只是 \r
...
$c = explode("\n",$content);
解决方案
你还需要检查" "和" ",因为这些也是有效终止空线。
当在规范形式、媒体亚型的"文本"型使用CRLF文线断裂。HTTP放宽这一要求,并允许运输的文字媒体与普通CR或如果单独代表断行时,它是一贯做为整个实体的身体。HTTP应用程序必须接受CRLF,裸CR,如果作为被代表的一线打破文字媒体的接收通过HTTP。
其他提示
头终止与一个双行没有空间之间(即一个空白线)。断行既可以是" "," "或只是" "。虽然后者并不是常见它仍然需要加以考虑。
也许你可以找到结束头一个经常的表情喜欢
list($headers) = preg_split('/(\r\n?|\n)(\r\n?|\n)/', $httpresponse);
不隶属于 StackOverflow