我無法使用simplexml_load_file函數從文件獲取XML。我曾嘗試使用Google,但其他人似乎在遇到實際錯誤或警告時遇到問題。我沒有得到任何錯誤,沒有警告,但是當我這樣做:PHP - 使用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 Version 5.3.2-1ubuntu4.5和libxml版本2.7.6。
謝謝!
-Travis
可以確認URL僅僅是通過'http:// my-url.com/xml.php'沒有任何'$ _GET'? – ajreal 2010-12-17 16:56:11