php SimpleXML - I/O 警告:无法加载外部实体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21661593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:20:15  来源:igfitidea点击:

SimpleXML - I/O warning : failed to load external entity

php

提问by Gavin Mannion

I'm trying to create a small application that will simply read an RSS feed and then layout the info on the page.

我正在尝试创建一个小型应用程序,该应用程序将简单地读取 RSS 提要,然后在页面上布置信息。

All the instructions I find make this seem simplistic but for some reason it just isn't working. I have the following

我发现的所有说明都使这看起来很简单,但由于某种原因它不起作用。我有以下

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

That's it, it then breaks on the third line with the following error

就是这样,然后它在第三行中断并出现以下错误

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.

I have turned on allow_url_fopen and allow_url_include in my settings but still nothing. I've tried multiple feeds that all end up with the same result?

我在我的设置中打开了 allow_url_fopen 和 allow_url_include 但仍然没有。我尝试了多个提要,但结果都一样?

I'm going mad here

我要疯了

回答by Amal Murali

simplexml_load_file()interprets an XML file(either a file on your disk or a URL) into an object. What you have in $feedis a string.

simplexml_load_file()XML 文件(磁盘上的文件或 URL)解释为对象。你所拥有的$feed是一个字符串。

You have two options:

您有两个选择:

  • Use file_get_contents()to get the XML feed as a string, and use e simplexml_load_string():

    $feed = file_get_contents('...');
    $items = simplexml_load_string($feed);
    
  • Load the XML feed directly using simplexml_load_file():

    $items = simplexml_load_file('...');
    
  • 用于file_get_contents()以字符串形式获取 XML 提要,并使用 e simplexml_load_string()

    $feed = file_get_contents('...');
    $items = simplexml_load_string($feed);
    
  • 使用simplexml_load_file()以下命令直接加载 XML 提要:

    $items = simplexml_load_file('...');
    

回答by Herlon Aguiar

You can also load the content with cURL, if file_get_contents insn't enabled on your server.

如果您的服务器上未启用 file_get_contents,您还可以使用 cURL 加载内容。

Example:

例子:

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

回答by Herlon Aguiar

this also works:

这也有效:

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

then I just run a forloop to grab the stuff from the nodes.

然后我只是运行一个 forloop 来从节点中获取东西。

like this:`

像这样:`

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;

***note that your node names will differ, obviously..and your HTML might be structured differently...also your loop might be set to higher or lower amount of results.

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

回答by Júnior Mendon?a

$url = 'http://legis.senado.leg.br/dadosabertos/materia/tramitando';
$xml = file_get_contents("xml->{$url}");
$xml = simplexml_load_file($url);