[英]php: get_headers Function Results Are Different
i using get_headers Function in PHP to request headers from website in local server return arrays put when use in my website Does not return arrays 我在PHP中使用get_headers函数从本地服务器中的网站请求网站的标头返回数组在我的网站中使用时不返回数组
examples for returns 退货示例
in local server 在本地服务器中
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 ) )
in real server 在真实服务器中
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 )
i wont to return arrays 我不会返回数组
thanks.... 谢谢....
There seems to be a difference in how PHP handles redirects on your local server and on the real server. PHP处理本地服务器和真实服务器上的重定向的方式似乎有所不同。 I think you would get arrays locally too, but for some reason get_headers() locally doesn't seem to follow redirects. 我认为您也可以在本地获取数组,但是由于某些原因,本地的get_headers()似乎不遵循重定向。
Is the PHP version same in both environments? 两种环境中的PHP版本是否相同?
There doesn't seem a reason for this. 似乎没有原因。 You should set the second parameter to a non-zero value in order to get an array 1 : 您应该将第二个参数设置为非零值以获得数组1 :
get_headers($url, 1);
If you do that, it should run the same anywhere, unless there is a bug in PHP itself or in the problematic server (both are rare cases for the occasional user). 如果这样做的话,它应该在任何地方都可以运行,除非PHP本身或有问题的服务器中有错误(这两种情况对于偶尔的用户都是很少见的)。
Note that get_headers
follows (multiple) redirects and stores the headers of each redirect as an array 2 : 请注意, 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"
}
...
The particular header values for redirects are stored successively, so it looks like Content-Type[0]
can be related to any of the Location
s, which makes the array format unusable to get the headers of each of the redirects correctly. 重定向的特定标头值是连续存储的,因此看起来Content-Type[0]
可以与任何Location
关联,这使得数组格式无法正确获取每个重定向的标头。 Line array format is not much better, since you will need to parse the headers. 行数组格式不是更好,因为您将需要解析标头。 But with the array format you can detect the last location etc. 但是使用数组格式,您可以检测到最后的位置等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.