如何解析响应报头在PHP? [英] How to parse response headers in PHP?
本文介绍了如何解析响应报头在PHP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我做了一个OAuth签名的请求到REST API并具有响应头在这样一个数组:
I have made an oauth signed request to a REST API and have the response headers in an array like so:
[0] => HTTP/1.1 200 OK
[1] => Cache-Control: private
[2] => Transfer-Encoding: chunked
[3] => Content-Type: text/html; charset=utf-8
[4] => Content-Location: https://***
[5] => Server: Microsoft-IIS/7.0
[6] => Set-Cookie: ASP.NET_SessionId=***; path=/; HttpOnly
[7] => X-AspNetMvc-Version: 2.0
[8] => oauth_token: ***
[9] => oauth_token_secret: ***
[10] => X-AspNet-Version: 4.0.30319
[11] => X-Powered-By: ASP.NET
[12] => Date: Sat, 15 Sep 2012 02:01:15 GMT
我试图找出如何解析的项目便于检索的头,比如HTTP状态code,内容位置,组oauth_token,和oauth_token_secret?
I am trying to figure out how to parse the headers for easy retrieval of items such as the HTTP status code, Content-Location, oauth_token, and oauth_token_secret?
推荐答案
您将需要遍历数组和检查 stripos函数()
来找到你的头寻找。在大多数情况下,你再爆炸()
在:
(限制为2产生的部分),但HTTP响应$ C $按c需要你发生爆炸的空间。
You'll need to iterate the array and check stripos()
to find the header you're looking for. In most cases, you then explode()
on :
(limiting to 2 resultant parts), but the HTTP response code will require you to explode on the spaces.
// Get any header except the HTTP response...
function getResponseHeader($header) {
foreach ($response as $key => $r) {
if (stripos($r, $header) !== FALSE) {
list($headername, $headervalue) = explode(":", $r, 2);
return trim($headervalue);
}
}
}
// example:
echo getResponseHeader("Content-Type");
// text/html; charset=utf-8
// Get the HTTP response code
foreach ($response as $key => $r) {
if (stripos($r, 'HTTP/1.1') !== FALSE) {
list(,$code, $status) = explode(' ', $r);
echo "Code: $code, Status: $status";
break;
}
}
这篇关于如何解析响应报头在PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文