2014-02-15 38 views
3

禁止404警告后,fopen函数正确填充$http_response_header变量,但返回错误句柄。是否有可能在php中使用fopen获取404页面内容?

$opts = array(
    'http'=>array(
    'method'=>"GET", 
    'header'=>"Accept-language: en\r\n" 
) 
); 
$context = stream_context_create($opts); 
$old_eh = set_error_handler (function() {}); 
$url = "https://wikimediafoundation.org/nonexisting"; 
$handle = fopen($url, 'r', false, $context); 
var_dump($http_response_header); 
var_dump($handle); 

但是,在浏览器中打开此页面显示有内容。我在这里被迫使用Curl吗?

也许我应该在$ context变量中加入一些选项来改变这种行为?

结果:

array(15) { 
    [0]=> string(22) "HTTP/1.1 404 Not Found" 
    [1]=> string(20) "Server: nginx/1.1.19" 
    [2]=> string(35) "Date: Sat, 15 Feb 2014 16:23:41 GMT" 
    [3]=> string(38) "Content-Type: text/html; charset=utf-8" 
    [4]=> string(20) "Content-Length: 2858" 
    [5]=> string(17) "Connection: close" 
    [6]=> string(40) "X-Powered-By: PHP/5.3.10-1ubuntu3.9+wmf1" 
    [7]=> string(48) "Cache-Control: s-maxage=2678400, max-age=2678400" 
    [8]=> string(78) "X-Wikimedia-Debug: prot=https:// serv=wikimediafoundation.org loc=/nonexisting" 
    [9]=> string(64) "Refresh: 5; url=https://wikimediafoundation.org/wiki/nonexisting" 
    [10]=> string(33) "X-Varnish: 2541084702, 3538479758" 
    [11]=> string(29) "Via: 1.1 varnish, 1.1 varnish" 
    [12]=> string(20) "Accept-Ranges: bytes" 
    [13]=> string(6) "Age: 0" 
    [14]=> string(50) "X-Cache: cp1068 miss (0), cp1068 frontend miss (0)" 
} 
bool(false) 
+0

'fopen()函数'只限于只是使所提供的URL的'GET'请求而无需任何进一步定制。 CURL不受PHP中的安全配置的影响,并允许进行大量定制。你为什么不想用cURL? –

+0

@AmalMurali:这......显然很不真实。我承认你可以在'curl'中继续_farther_,但是'fopen''''''''''''''''''''''''''''''''''''''''''''''''''''''。 'OP'甚至在他的问题中给你一个提示:用'stream_context'。 – Wrikken

回答

4

as per the manual设置ignore_errorstrue

$opts = array(
    'http' => array(
     'method' => "GET", 
     'header' => "Accept-language: en\r\n", 
     'ignore_errors' => true 
) 
); 
相关问题