php simplexml_load_file 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6529533/
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-26 00:39:20  来源:igfitidea点击:

simplexml_load_file not working

phpapachesimplexmlfile-get-contents

提问by Mark

I have this piece of code below which works fine on my remote hosted server, but isnt for some reason working on my local linux machine. Ive tried using file_get_contents as well to get the restful service but it also returns false.

我有下面这段代码,它在我的远程托管服务器上运行良好,但由于某种原因不能在我的本地 linux 机器上运行。我也尝试过使用 file_get_contents 来获得宁静的服务,但它也返回 false。

Does anyone know Why this is happening?

有谁知道为什么会这样?

thanks :)

谢谢 :)

$xml_data = simplexml_load_file("****");

if ($xml == FALSE)
{
  echo "Failed loading XML\n";

  foreach (libxml_get_errors() as $error) 
  {
    echo "\t", $error->message;
  }   
} 

回答by Jigar Tank

You are getting this error because remote file access has been disabled on your server. An alternative to this is using CURL.

您收到此错误是因为您的服务器上已禁用远程文件访问。另一种方法是使用 CURL。

Use my code below to use CURL:

使用我下面的代码来使用 CURL:

function produce_XML_object_tree($raw_XML) {
    libxml_use_internal_errors(true);
    try {
        $xmlTree = new SimpleXMLElement($raw_XML);
    } catch (Exception $e) {
        // Something went wrong.
        $error_message = 'SimpleXMLElement threw an exception.';
        foreach(libxml_get_errors() as $error_line) {
            $error_message .= "\t" . $error_line->message;
        }
        trigger_error($error_message);
        return false;
    }
    return $xmlTree;
}

$xml_feed_url = '******';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $xml_feed_url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$xml = curl_exec($ch);
curl_close($ch);

$cont = produce_XML_object_tree($xml);

Now use $contas an object to access different nodes in the xml.

现在$cont用作对象来访问xml中的不同节点。

回答by Michael Berkowski

Make sure you have allow_url_fopenturned on in your php.ini

确保您已allow_url_fopen在您的php.ini

http://php.net/manual/filesystem.configuration.php

http://php.net/manual/filesystem.configuration.php

回答by Dejacore

Well I had same issue and though I would post this to assist anyone who may have not tried this solution yet.

好吧,我遇到了同样的问题,尽管我会发布此信息以帮助可能尚未尝试过此解决方案的任何人。

I had a PHP script which worked fine locally, but when using it on a client server running plesk it would not work and failed when trying to grab the external xml file.

我有一个在本地运行良好的 PHP 脚本,但是当在运行 plesk 的客户端服务器上使用它时,它在尝试获取外部 xml 文件时不起作用并失败。

I was trying to reference an external xml file from a php script. The server I was using was running plesk. Before considering changing host, All I simply did was update the settings for PHP on the server to run as an Apache Module instead of FastCGI.

我试图从 php 脚本中引用外部 xml 文件。我使用的服务器正在运行 plesk。在考虑更换主机之前,我所做的只是更新服务器上 PHP 的设置,使其作为 Apache 模块而不是 FastCGI 运行。

error message which I was receiving (example):

我收到的错误消息(示例):

Warning: simplexml_load_file(url) [function.simplexml-load-file]: failed to open stream: Permission denied

警告:simplexml_load_file(url) [function.simplexml-load-file]:无法打开流:权限被拒绝

This resolved the issue in my case.

这解决了我的问题。

I used following reports settings in the PHP script:

我在 PHP 脚本中使用了以下报告设置:

assert_options(ASSERT_ACTIVE, 1);
assert_options(ASSERT_BAIL, 1);
assert_options(ASSERT_QUIET_EVAL, 1);
error_reporting(E_ALL);
ini_set('display_errors', 1);

回答by Eranda

use like this

像这样使用

$xml = simplexml_load_file('http://localhost/test/123.xml');

foreach ($xml->children() as $child) {
    $remoteCount[$child->getName()] = $child;

}
var_dump($remoteCount);

回答by Gregory

Change: if ($xml == FALSE)to if ($xml === FALSE)(source).

更改:if ($xml == FALSE)if ($xml === FALSE))。

回答by George SEDRA

I had the same problem it's just a stupid undeclared point in the simplexml

我遇到了同样的问题,它只是simplexml 中一个愚蠢的未声明点

the xml file format should have a container tag, so, you just have to put a parent tag containing all your data like this:

xml 文件格式应该有一个容器标签,因此,您只需放置一个包含所有数据的父标签,如下所示:

<?xml version="1.0">
<data>
    ...all your file content here...
</data>