is it possible to get 404 page content using fopen

2019-05-12 07:48发布

After supressing 404 warning the fopen function correctly populates $http_response_header variable but returns false handle.

$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);

But opening this page in browser shows that there is a content. Am I forced to use Curl in here ?

Maybe I should put some options in $context variable to change this behavior ?

result:

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)

1条回答
叛逆
2楼-- · 2019-05-12 08:46

as per the manual set ignore_errors to true:

$opts = array(
  'http' => array(
      'method' => "GET",
      'header' => "Accept-language: en\r\n",
      'ignore_errors' => true
  )
);
查看更多
登录 后发表回答