PHP - 使用 simplexml_load_file 获取 XML 时出现问题
我在使用 simplexml_load_file 函数从文件中获取 XML 时遇到问题。我尝试过谷歌搜索,但其他人在收到实际错误或警告时似乎都遇到了问题。我没有收到任何错误,也没有警告,但是当我这样做时:
$sims = simplexml_load_file("http://my-url.com/xml.php") or die("Unable to load XML file!");
var_dump($sims);
输出是:
object(SimpleXMLElement)#1 (1) {
[0]=>
string(1) "
"
}
但是,如果我这样做:
$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;
输出是:
<?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>
我已经通过这样做让它工作了:
$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);
哪个输出:
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"
}
}
我只是想知道为什么第一种方法不起作用?我在 Ubuntu Server 10.04 上运行 PHP 版本 5.3.2-1ubuntu4.5 和 libxml 版本 2.7.6。
谢谢!
-特拉维斯
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:
$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.
Thanks!
-Travis
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信这可能是因为您的 xml 内容位于 .php 扩展文件中。您需要将 http 标头设置为 xml。
将其放在负责输出 xml 的 php 脚本中输出 xml 之前。 (位于“http://my-url.com/xml.php”的那个)
http://www.satya-weblog.com/2008/02/header-for-xml-content-in-php-file.html
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.
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")
http://www.satya-weblog.com/2008/02/header-for-xml-content-in-php-file.html
感谢您的快速回复。
@ajreal - 你走在正确的轨道上。事实证明,这是我自己在查询字符串中犯的愚蠢错误,出于某种原因,它在通过 cURL 或在浏览器中调用它时有效,但在通过 simplexml_load_file 调用时不起作用。抱歉浪费您的时间!
-特拉维斯
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!
-Travis