天天看點

php的file_get_contents函數通路URL顯示響應頭php的file_get_contents函數通路URL顯示響應頭

php的file_get_contents函數通路URL顯示響應頭

作者:matrix 被圍觀: 5,529 次 釋出時間:2014-10-01 分類:零零星星 | 6 條評論 »

這是一個建立于 2892 天前的主題,其中的資訊可能已經有所發展或是發生改變。

在用 file_get_contents 通路 http 時,stream wrapper 會把響應頭放到目前作用域下的 $http_response_header 數組變量裡。

是以說變量$http_response_header就儲存了需要的響應頭,輸出這個變量也就能拿到響應頭。

file_get_contents('http://www.hhtjim.com/');
print_r($http_response_header);//輸出響應頭内容           

複制

參考:

http://www.jbxue.com/article/16319.html

PS:

平時用file_get_contents()函數讀取url的網頁内容,還不了解原來這玩意還有很多參數可以設定。

<?php
$url = 'http://www.baidu.com';
$opts = array('http'=>array('header' => "User-Agent:Mozilla/5.0 (Windows NT 6.2; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0\r\n"));
$context = stream_context_create($opts);
$data = file_get_contents($url,false,$context);
print_r($data);           

複制

說明:

在sae上測試無法抓取(包括curl),才改用file_get_contents函數,并且添加User-Agent參數。

參考:http://blog.sina.com.cn/s/blog_4ae555810101cuef.html

http://stackoverflow.com/questions/22498581/php-file-get-contents-500-internal-server-error-in-php