simplexml_load_file not loading [duplicate]

2019-08-24 05:06发布

This question already has an answer here:

This link to an rss feed doesn't load with simplexml_load_file.

The link is a valid RSS feed and no it isn't a permission problem everything else loads.

2条回答
Luminary・发光体
2楼-- · 2019-08-24 05:24

The rss feed is gziped. This should do the trick:

$content =  file_get_contents("http://www.nationnews.com/site/feed/");
$rss = simplexml_load_string(gzinflate(substr($content,10,-8)));

See PHP: Call to undefined function gzdecode() for further details regarding the gzinflate.

查看更多
The star\"
3楼-- · 2019-08-24 05:32

First of all you need to enable error logging and/or reporting to find out more. Also you can check some parameters from the return value and the remote request:

$result = simplexml_load_file($url);

var_dump($result, $http_response_header);

This will tell you that loading failed, the error messages tell you why it failed:

PHP Warning: simplexml_load_file(): http://www.nationnews.com/site/feed/:1: parser error : Start tag expected, '<' not found in /example.php on line 10
PHP Warning: simplexml_load_file(): in /example.php on line 10
PHP Warning: simplexml_load_file(): ^ in /example.php on line 10

And the $http_response_header also show you the picture what has been returned from that host:

array(23) {
  [0]=> string(15) "HTTP/1.0 200 OK"
  [1]=> string(35) "Date: Wed, 20 Feb 2013 10:56:11 GMT"
  [2]=> string(30) "Server: Apache/2.2.15 (CentOS)"
  [3]=> string(23) "X-Powered-By: PHP/5.3.3"
  [4]=> string(56) "Set-Cookie: PHPSESSID=qeaq20mrvrc2u4c403sou6oro2; path=/"
  [5]=> string(38) "Expires: Wed, 20 Feb 2013 08:13:01 GMT"
  [6]=> string(50) "Cache-Control: no-store, no-cache, must-revalidate"
  [7]=> string(16) "Pragma: no-cache"
  [8]=> string(84) "Set-Cookie: exp_last_visit=1046015771; expires=Thu, 20-Feb-2014 10:56:11 GMT; path=/"
  [9]=> string(87) "Set-Cookie: exp_last_activity=1361375771; expires=Thu, 20-Feb-2014 10:56:11 GMT; path=/"
  [10]=> string(89) "Set-Cookie: exp_tracker=a%3A1%3A%7Bi%3A0%3Bs%3A11%3A%22%2Fsite%2Ffeed%2F%22%3B%7D; path=/"
  [11]=> string(44) "Last-Modified: Wed, 20 Feb 2013 07:13:01 GMT"
  [12]=> string(40) "Cache-Control: post-check=0, pre-check=0"
  [13]=> string(21) "Vary: Accept-Encoding"
  [14]=> string(16) "imagetoolbar: no"
  [15]=> string(17) "Connection: close"
  [16]=> string(37) "Content-Type: text/xml; charset=utf-8"
  [17]=> string(76) "Set-Cookie: cookiesession1=HTEVZV0HJNK2HARUL2QBDADH8RXYESJB;Path=/;HttpOnly "
  [18]=> string(109) "Set-Cookie: exp_last_visit_cookiesession2=tSzDt6/k20k=;Expires=Thu, 20-Feb-2014 10:56:11 GMT;Path=/;HttpOnly "
  [19]=> string(112) "Set-Cookie: exp_last_activity_cookiesession2=e+FVcqI8+Ck=;Expires=Thu, 20-Feb-2014 10:56:11 GMT;Path=/;HttpOnly "
  [20]=> string(68) "Set-Cookie: exp_tracker_cookiesession2=w+13lT4TxY0=;Path=/;HttpOnly "
  [21]=> string(22) "Content-Encoding: gzip"
  [22]=> string(20) "content-length: 2463"
}

According to the HTTP specs that you make use of by using a HTTP uri, the content encoding is:

[21]=> string(22) "Content-Encoding: gzip"

This is not supported by PHP out of the box with it's HTTP Wrapper so you need to work around that your own

For example by using the zlib Stream Wrapper::

$result = simplexml_load_file('compress.zlib://' . $url);

Or with the help of the gzdecode function:

$result = simplexml_load_string(gzdecode(file_get_contents($url)));

Full example code with all the variants:

$url = 'http://www.nationnews.com/site/feed/';


// stream wrapper:

$result = simplexml_load_file('compress.zlib://' . $url);

var_dump($result, $http_response_header);


// gzdecode:

$result = simplexml_load_string(gzdecode(file_get_contents($url)));

var_dump($result, $http_response_header);


// none (the error case):

$result = simplexml_load_file($url);

var_dump($result, $http_response_header);

Related Questions:

And the following PHP Bugreports are related (just picked some, this is likely not complete):

查看更多
登录 后发表回答