[英]php: get_headers Function Results Are Different
我在PHP中使用get_headers函数从本地服务器中的网站请求网站的标头返回数组在我的网站中使用时不返回数组
退货示例
在本地服务器中
Array ( [0] => HTTP/1.1 301 Moved [Server] => Array ( [0] => nginx/0.7.42 [1] => Apache/2.2.11 (Unix) mod_ssl/2.2.11 OpenSSL/0.9.8e-fips-rhel5 mod_auth_passthrough/2.1 mod_bwlimited/1.4 [2] => Apache/2.2.11 (Unix) mod_ssl/2.2.11 OpenSSL/0.9.8e-fips-rhel5 mod_auth_passthrough/2.1 mod_bwlimited/1.4 [3] => Microsoft-IIS/7.0 ) [Content-Type] => Array ( [0] => text/html; charset=utf-8 [1] => text/html; charset=iso-8859-1 [2] => text/html [3] => text/html; charset=utf-8 ) [Location] => Array ( [0] => http//3.ly/aXP [1] => http//3.ly/aXP/ [2] => http//stackoverflow.com ) [MIME-Version] => 1.0 [Content-Length] => Array ( [0] => 277 [1] => 376 [2] => 0 [3] => 122213 ) )
在真实服务器中
Array ( [0] => HTTP/1.1 301 Moved [Server] => nginx/0.7.42 [Date] => Sat, 10 Oct 2009 03:15:32 GMT [Content-Type] => text/html; charset=utf-8 [Connection] => keep-alive [Location] => http//3.ly/aXP [MIME-Version] => 1.0 [Content-Length] => 277 )
我不会返回数组
谢谢....
PHP处理本地服务器和真实服务器上的重定向的方式似乎有所不同。 我认为您也可以在本地获取数组,但是由于某些原因,本地的get_headers()似乎不遵循重定向。
两种环境中的PHP版本是否相同?
似乎没有原因。 您应该将第二个参数设置为非零值以获得数组1 :
get_headers($url, 1);
如果这样做的话,它应该在任何地方都可以运行,除非PHP本身或有问题的服务器中有错误(这两种情况对于偶尔的用户都是很少见的)。
请注意, get_headers
遵循(多个)重定向并将每个重定向的标头存储为数组2 :
array(11) {
[0]=>
string(30) "HTTP/1.0 301 Moved Permanently"
["Location"]=> string(22) "http://www.google.com/"
["Content-Type"]=> array(2) {
[0]=> string(24) "text/html; charset=UTF-8"
[1]=> string(29) "text/html; charset=ISO-8859-1"
}
...
重定向的特定标头值是连续存储的,因此看起来Content-Type[0]
可以与任何Location
关联,这使得数组格式无法正确获取每个重定向的标头。 行数组格式不是更好,因为您将需要解析标头。 但是使用数组格式,您可以检测到最后的位置等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.