Php SimpleXML-I/O警告:加载外部实体失败

Php SimpleXML-I/O警告:加载外部实体失败,php,Php,我正在尝试创建一个小应用程序,它只需读取RSS提要,然后在页面上布局信息 我发现所有的说明都让这看起来很简单,但出于某种原因,它就是不起作用。我有以下几点 include_once(ABSPATH.WPINC.'/rss.php'); $feed = file_get_contents('http://feeds.bbci.co.uk/sport/0/football/rss.xml?edition=int'); $items = simplexml_load_file($feed); 就这样

我正在尝试创建一个小应用程序,它只需读取RSS提要,然后在页面上布局信息

我发现所有的说明都让这看起来很简单,但出于某种原因,它就是不起作用。我有以下几点

include_once(ABSPATH.WPINC.'/rss.php');
$feed = file_get_contents('http://feeds.bbci.co.uk/sport/0/football/rss.xml?edition=int');
$items = simplexml_load_file($feed);
就这样,它在第三行中断,出现以下错误

Error: [2] simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "<?xml version="1.0" encoding="UTF-8"?> <?xm
The rest of the XML file is shown.
错误:[2]simplexml加载文件()[函数.simplexml加载文件]:I/O警告:加载外部实体失败“
simplexml加载文件()
XML文件(磁盘上的文件或URL)解释为对象。
$feed
中包含的是字符串

您有两个选择:

  • 使用
    file\u get\u contents()

  • 使用以下命令直接加载XML提要:


如果服务器上未启用文件获取内容,则也可以使用cURL加载内容

例如:

$ch = curl_init();  

curl_setopt($ch,CURLOPT_URL,"http://feeds.bbci.co.uk/sport/0/football/rss.xml?edition=int");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);

$output = curl_exec($ch);

curl_close($ch);

$items = simplexml_load_string($output);
这也适用于:

$url = "http://www.some-url";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$xmlresponse = curl_exec($ch);
$xml=simplexml_load_string($xmlresponse);
然后我运行一个forloop从节点中获取内容

像这样:`

for($i = 0; $i < 20; $i++) {
$title = $xml->channel->item[$i]->title;
$link = $xml->channel->item[$i]->link;
$desc = $xml->channel->item[$i]->description;
$html .="<div><h3>$title</h3>$link<br />$desc</div><hr>";
}
echo $html;
($i=0;$i<20;$i++)的
{
$title=$xml->channel->item[$i]->title;
$link=$xml->channel->item[$i]->link;
$desc=$xml->channel->item[$i]->description;
$html.=“$title$link
$desc
”; } echo$html;

***请注意,您的节点名称显然会有所不同..而且您的HTML结构可能会有所不同..而且您的循环可能会设置为更高或更低的结果量。

simplexml\u load\u file()
需要文件名!用于已加载的数据非常简单而且非常有效..感谢关于使用file\u get\u contents()的说明.I遇到相同错误(I/O警告:加载外部实体失败)并发现这是由我的PHP配置引起的。请检查PHP.ini中的upload_max_filesize和post_max_size,并确保它们至少与您试图解析的文件一样大。虽然此代码片段可能会解决问题,但它没有解释为什么或如何回答问题。请检查,因为这确实有助于提高您的质量您的帖子。请记住,您将在将来回答读者的问题,而这些人可能不知道您代码建议的原因。标记者/审阅者:Júnior,ché,为您的答案添加一些解释,以便我们对其进行投票:)
$url = 'http://legis.senado.leg.br/dadosabertos/materia/tramitando';
$xml = file_get_contents("xml->{$url}");
$xml = simplexml_load_file($url);
for($i = 0; $i < 20; $i++) {
$title = $xml->channel->item[$i]->title;
$link = $xml->channel->item[$i]->link;
$desc = $xml->channel->item[$i]->description;
$html .="<div><h3>$title</h3>$link<br />$desc</div><hr>";
}
echo $html;
$url = 'http://legis.senado.leg.br/dadosabertos/materia/tramitando';
$xml = file_get_contents("xml->{$url}");
$xml = simplexml_load_file($url);