【发布时间】:2011-06-07 18:35:24
【问题描述】:
我正在通过 cUrl 接收使用 PHP 生成的 XML 代码:
$c = curl_init("http://www.domain.com/script.php");
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$xmlstr = curl_exec($c);
如果我回显 $xmlstr 变量,它会显示以下内容:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<XGetPasoParadaREGResponse xmlns="http://tempuri.org/">
<XGetPasoParadaREGResult>
<PasoParada><cabecera>false</cabecera>
<e1>
<minutos>1</minutos>
<metros>272</metros>
<tipo>NORMAL</tipo>
</e1>
<e2>
<minutos>7</minutos>
<metros>1504</metros>
<tipo>NORMAL</tipo>
</e2><linea>28
</linea>
<parada>376</parada>
<ruta>PARQUE ALCOSA</ruta>
</PasoParada>
</XGetPasoParadaREGResult><status>1</status></XGetPasoParadaREGResponse>
</soap:Body>
</soap:Envelope>
哪个是正确的,并且在脚本中生成的相同。但是,如果我尝试执行以下操作
$xml = simplexml_load_string($xmlstr);
if (!is_object($xml))
throw new Exception('Reading XML error',1001);
echo $xml->e1->minutos;
不显示任何内容,并且 print_r 对象打印一个空对象。有什么问题?
【问题讨论】:
-
var_dump($xml)得到什么? -
object(SimpleXMLElement)#1 (0) { } 但字符串很好,如果保存为 XML,每个浏览器都能很好地解释。