simplexml_load_file未加载 [英] simplexml_load_file not loading

查看:337
本文介绍了simplexml_load_file未加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此指向rss feed的链接未随simplexml_load_file一起加载.

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

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

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

推荐答案

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

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警告:simplexml_load_file():http://www.nationnews.com/site/feed/:1:解析器错误:起始标签应为'<'在第10行的/example.php中找不到
PHP警告:第10行/example.php中的simplexml_load_file():
PHP警告:simplexml_load_file():第10行/example.php中的^

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

$http_response_header还会向您显示该主机返回的图片:

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

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

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"

PHP的HTTP Wrapper不提供现成的支持,因此您需要自己解决此问题

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

例如,使用 zlib Stream Wrapper::

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

或借助 gzdecode 函数:

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

相关问题:

  • Uncompress a gzip file from CURL, on php
  • Uncompress gzip compressed http response
  • PHP: Call to undefined function gzdecode()

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

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

  • Bug #52926 - stream_context_set_default() does not work as intended with stream chaining (26 Sep 2010)
  • Bug #47925 - PHPClient can't decompress response (transposed uncompress methods?) (8 Apr 2009)
  • Doc Bug #29045 - gzopen for URL (7 Jul 2005)
  • Request #28051 - gzip accept-encoding for HTTP request (19 Apr 2004)

这篇关于simplexml_load_file未加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆