【问题标题】:simplexml_load_file not loading [duplicate]simplexml_load_file 未加载 [重复]
【发布时间】:2013-02-04 20:12:41
【问题描述】:

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

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

【问题讨论】:

  • 请发布您正在使用的 PHP 代码 (sn-p),以及您想要的预期结果(要解析的字段)。

标签: http gzip simplexml php


【解决方案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():在 /example.php 第 10 行
PHP 警告:simplexml_load_file(): ^ in /example.php on line 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相关(随便挑一些,这可能不完整):

【讨论】:

  • 我会确保启用错误检查
  • @user1807206:是的,对于开发,启用所有错误。这将为您节省大量时间寻找错误的地方。您可能会发现这很有用:How to get useful error messages in PHP? - 它有点宽泛并且有不同的答案,但它在某种程度上显示了存在的不同可能性。更多动手操作是the answer I left in it
【解决方案2】:

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

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

有关 gzinflate 的更多详细信息,请参阅 PHP: Call to undefined function gzdecode()

【讨论】:

  • 谢谢,这很容易。在任何地方都找不到答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-20
  • 1970-01-01
  • 1970-01-01
  • 2020-05-07
  • 1970-01-01
相关资源
最近更新 更多