0

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.

hakre
  • 193,403
  • 52
  • 435
  • 836
user1807206
  • 23
  • 1
  • 4
  • Please post the PHP code (snippet) you are using, as well as the intended results you want (which fields to parse). – ionFish Feb 19 '13 at 23:25

2 Answers2

1

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.

Community
  • 1
  • 1
Corneliu
  • 1,110
  • 1
  • 10
  • 16
1

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):

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
  • I will make sure to enable error checking – user1807206 Feb 20 '13 at 21:20
  • @user1807206: Yes, for development, enable all errors. This will save you a lot of time looking in the wrong places. You might find this useful: [How to get useful error messages in PHP?](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php) - it's a bit broad and has different answers, but it somewhat shows the different possibilities there are. More Hands-On is [the answer I left in it](http://stackoverflow.com/a/14504459/367456). – hakre Feb 21 '13 at 08:05