0

这个指向 rss 提要的链接没有加载simplexml_load_file.

该链接是一个有效的 RSS 提要,不,它不是其他所有加载的权限问题。

4

2 回答 2

1

RSS 提要已压缩。这应该可以解决问题:

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

有关 gzinflate 的更多详细信息,请参阅PHP:调用未定义的函数 gzdecode()

于 2013-02-19T23:57:12.230 回答
1

首先,您需要启用错误记录和/或报告以了解更多信息。您还可以从返回值和远程请求中检查一些参数:

$result = simplexml_load_file($url);

var_dump($result, $http_response_header);

这将告诉您加载失败,错误消息会告诉您失败的原因

PHP 警告:simplexml_load_file(): http://www.nationnews.com/site/feed/:1: 解析器错误:需要开始标记,在第 10 行的 /example.php 中找不到“<”
PHP 警告:simplexml_load_file():在第 10 行的 /example.php 中
PHP 警告:simplexml_load_file(): ^ 在 /example.php 第 10 行

并且$http_response_header还向您展示了从该主机返回的图片:

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"
}

根据您通过使用 HTTP uri 使用的 HTTP 规范,内容编码为:

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

PHP 开箱即用的 HTTP Wrapper 不支持此功能,因此您需要自己解决

例如,通过使用zlib Stream Wrapper:

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

或者在gzdecode函数的帮助下:

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

包含所有变体的完整示例代码:

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

相关问题:

和以下 PHP Bugreports 相关(只是挑了一些,这可能不完整):

于 2013-02-20T10:59:34.470 回答