php: get_headers 函数结果不同
我在 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 )
我不会返回数组,
谢谢......
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
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....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
PHP 在本地服务器和真实服务器上处理重定向的方式似乎有所不同。我认为您也会在本地获取数组,但由于某种原因,本地 get_headers() 似乎不遵循重定向。
两个环境中的 PHP 版本是否相同?
There seems to be a difference in how PHP handles redirects on your local server and on the real server. I think you would get arrays locally too, but for some reason get_headers() locally doesn't seem to follow redirects.
Is the PHP version same in both environments?
这似乎没有理由。您应该将第二个参数设置为非零值才能获取数组1:
如果你这样做,它在任何地方都应该运行相同的,除非 PHP 本身或有问题的服务器存在错误(对于偶尔的用户来说,这两种情况都是罕见的)。
请注意,
get_headers
遵循(多个)重定向,并将每个重定向的标头存储为数组2:重定向的特定标头值连续存储,因此看起来像
Content-Type[0]
可以与任何Location
相关,这使得数组格式无法正确获取每个重定向的标头。行数组格式也好不了多少,因为您需要解析标头。但使用数组格式,您可以检测最后一个位置等。There doesn't seem a reason for this. You should set the second parameter to a non-zero value in order to get an array1:
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).
Note that
get_headers
follows (multiple) redirects and stores the headers of each redirect as an array2:The particular header values for redirects are stored successively, so it looks like
Content-Type[0]
can be related to any of theLocation
s, which makes the array format unusable to get the headers of each of the redirects correctly. 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.