PHP -使用simplexml_load_file获取XML的问题

时间:2022-03-06 00:12:15

I'm having trouble getting the XML from a file using the simplexml_load_file function. I have tried googling, but everyone else seems to have problems when they get an actual error or warning. I get no error and no warnings, but when I do this:

我无法使用simplexml_load_file函数从文件中获取XML。我曾尝试过谷歌搜索,但其他人在收到实际错误或警告时似乎都有问题。我不会得到任何错误和警告,但当我这样做时:

$sims = simplexml_load_file("http://my-url.com/xml.php") or die("Unable to load XML file!");
var_dump($sims);

the output is:

的输出是:

object(SimpleXMLElement)#1 (1) {
  [0]=>
  string(1) "
"
}



However, if I do this:

然而,如果我这样做:

$ch = curl_init("http://my-url.com/xml.php");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
echo $output;

the output is:

的输出是:

<?xml version="1.0"?>
<simulators>
    <simulator>
        <mac>00-1A-4D-93-27-EC</mac>
        <friendlyName>a Travis Desk</friendlyName>
        <roundSessions>2</roundSessions>
        <rangeSessions>0</rangeSessions>
        <timePlayed>00:03:21</timePlayed>
    </simulator>
</simulators>



I have gotten it to work by doing this:

我这样做是为了让它工作:

$ch = curl_init("http://my-url.com/xml.php");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);

$sims = simplexml_load_string($output) or die("Unable to load XML file!");
var_dump($sims);

which outputs:

输出:

object(SimpleXMLElement)#1 (1) {
  ["simulator"]=>
  object(SimpleXMLElement)#2 (5) {
    ["mac"]=>
    string(17) "00-1A-4D-93-27-EC"
    ["friendlyName"]=>
    string(13) "a Travis Desk"
    ["roundSessions"]=>
    string(1) "2"
    ["rangeSessions"]=>
    string(1) "0"
    ["timePlayed"]=>
    string(8) "00:03:21"
  }
}

I'm just wondering why the first method didn't work? I have PHP Version 5.3.2-1ubuntu4.5 and libxml Version 2.7.6 running on Ubuntu Server 10.04.

我想知道为什么第一种方法不起作用?我有在Ubuntu服务器10.04上运行的PHP版本5.3.2-1ubuntu4.5和libxml版本2.7.6。

Thanks!

谢谢!

-Travis

特拉维斯

2 个解决方案

#1


1  

I believe it may be because your xml content is located within a .php extension file. You need to set the http header to xml.

我认为这可能是因为您的xml内容位于.php扩展文件中。您需要将http头设置为xml。

header ("Content-type: text/xml");

Put that before the xml is outputted in your php script that is responsible for spitting out the xml. (the one at "http://my-url.com/xml.php")

在php脚本中输出负责输出xml的xml之前,请将其放在这里。(“http://my-url.com/xml.php”)

http://www.satya-weblog.com/2008/02/header-for-xml-content-in-php-file.html

http://www.satya-weblog.com/2008/02/header-for-xml-content-in-php-file.html

#2


-1  

Thanks for the quick responses.

谢谢你的快速回复。

@ajreal - you were on the right track. Turns out it was my own dumb mistake in the querystring that, for some reason, worked when calling it via cURL or in the browser, but didn't work via simplexml_load_file. Sorry for wasting your time!

@ajreal -你走对了路。原来这是我自己在querystring中犯的愚蠢错误,出于某种原因,它在通过cURL或浏览器调用时起作用,但在simplexml_load_file中不起作用。对不起,耽误您时间了!

-Travis

特拉维斯

#1


1  

I believe it may be because your xml content is located within a .php extension file. You need to set the http header to xml.

我认为这可能是因为您的xml内容位于.php扩展文件中。您需要将http头设置为xml。

header ("Content-type: text/xml");

Put that before the xml is outputted in your php script that is responsible for spitting out the xml. (the one at "http://my-url.com/xml.php")

在php脚本中输出负责输出xml的xml之前,请将其放在这里。(“http://my-url.com/xml.php”)

http://www.satya-weblog.com/2008/02/header-for-xml-content-in-php-file.html

http://www.satya-weblog.com/2008/02/header-for-xml-content-in-php-file.html

#2


-1  

Thanks for the quick responses.

谢谢你的快速回复。

@ajreal - you were on the right track. Turns out it was my own dumb mistake in the querystring that, for some reason, worked when calling it via cURL or in the browser, but didn't work via simplexml_load_file. Sorry for wasting your time!

@ajreal -你走对了路。原来这是我自己在querystring中犯的愚蠢错误,出于某种原因,它在通过cURL或浏览器调用时起作用,但在simplexml_load_file中不起作用。对不起,耽误您时间了!

-Travis

特拉维斯