[英]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.