我在访问使用simplexml_load_string加载的xml数据时遇到问题。
首先,使用此API的curl检索数据 http://aesitelink.com/dataservice/DataServiceapi.asmx?op=GetInverterData
预期答案具有以下语法:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="DataService.API">string</string>
举个例子,我从卷曲请求中得到以下结果
<rws_response><InverterData InverterId='66531' RecordsLeft='0'><Data Time='1448273400'><p i='PAC' dec='1'>22599</p><p i='IAC' dec='1'>99</p><p i='UAC' dec='1'>2279</p><p i='FAC' dec='2'>5002</p><p i='PDC' dec='1'>23545</p><p i='IDC' dec='1'>37</p><p i='UDC' dec='1'>6442</p><p i='Temp1' dec='1'>244</p><p i='Temp2' dec='1'>331</p><p i='Energy' dec='0'>2</p><p i='TotalEnergy' dec='1'>698351</p><p i='State' dec='0'>4</p></Data><Data Time='1448274000'><p i='PAC' dec='1'>29162</p><p i='IAC' dec='1'>127</p><p i='UAC' dec='1'>2296</p><p i='FAC' dec='0'>50</p><p i='PDC' dec='1'>30099</p><p i='IDC' dec='1'>47</p><p i='UDC' dec='1'>6471</p><p i='Temp1' dec='1'>248</p><p i='Temp2' dec='1'>335</p><p i='Energy' dec='1'>24</p><p i='TotalEnergy' dec='1'>698355</p><p i='State' dec='0'>4</p></Data></InverterData></rws_response>
然后,我使用simplexml_load_string函数(我在阅读的几个帖子中建议使用trim):
$xml = simplexml_load_string( trim($xmlString) );
echo '<pre>';
print_r($xml);
echo '</pre>';
结果,在$ xml var中,我得到了
<pre>SimpleXMLElement Object
(
[0] => <rws_response><inverterdata inverterid="66531" recordsleft="0"><data time="1448273400"><p i="PAC" dec="1">22599</p><p i="IAC" dec="1">99</p><p i="UAC" dec="1">2279</p><p i="FAC" dec="2">5002</p><p i="PDC" dec="1">23545</p><p i="IDC" dec="1">37</p><p i="UDC" dec="1">6442</p><p i="Temp1" dec="1">244</p><p i="Temp2" dec="1">331</p><p i="Energy" dec="0">2</p><p i="TotalEnergy" dec="1">698351</p><p i="State" dec="0">4</p></data><data time="1448274000"><p i="PAC" dec="1">29162</p><p i="IAC" dec="1">127</p><p i="UAC" dec="1">2296</p><p i="FAC" dec="0">50</p><p i="PDC" dec="1">30099</p><p i="IDC" dec="1">47</p><p i="UDC" dec="1">6471</p><p i="Temp1" dec="1">248</p><p i="Temp2" dec="1">335</p><p i="Energy" dec="1">24</p><p i="TotalEnergy" dec="1">698355</p><p i="State" dec="0">4</p></data></inverterdata></rws_response>
)
</pre>
我的目标是浏览'rws_response'xml结构并从'p'标记的元素中获取信息。 我尝试使用$ xml-&gt; {'0'}访问内容,但结果是
print_r($xml->{'0'});
为空
<pre>SimpleXMLElement Object
(
)
</pre>
我该怎么办? 感谢您的支持。
答案 0 :(得分:0)
与另一个大脑合作是要走的路(感谢Martin; o)
我应该更多地关注&#39; =&gt;&#39;标志。 实际上是答案中的字符串
<string xmlns="DataService.API">string</string>
实际上是作为字符串处理的!不开玩笑。 因此,我不得不重新应用simplexml_load_string函数。
我在本地下载了服务器应答,以便于演示并避免显示凭据。 result.xml文件包含以下内容:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="DataService.API"><rws_response><InverterData InverterId='66531' RecordsLeft='0'><Data Time='1448440200'><p i='PAC' dec='0'>0</p><p i='IAC' dec='0'>0</p><p i='UAC' dec='0'>230</p><p i='FAC' dec='0'>50</p><p i='PDC' dec='0'>0</p><p i='IDC' dec='0'>0</p><p i='UDC' dec='1'>6262</p><p i='Temp1' dec='1'>154</p><p i='Temp2' dec='1'>212</p><p i='Energy' dec='0'>0</p><p i='TotalEnergy' dec='1'>698929</p><p i='State' dec='0'>2</p></Data><Data Time='1448440800'><p i='PAC' dec='1'>3894</p><p i='IAC' dec='1'>17</p><p i='UAC' dec='1'>2295</p><p i='FAC' dec='2'>4999</p><p i='PDC' dec='0'>429</p><p i='IDC' dec='1'>8</p><p i='UDC' dec='1'>5476</p><p i='Temp1' dec='1'>158</p><p i='Temp2' dec='1'>238</p><p i='Energy' dec='0'>0</p><p i='TotalEnergy' dec='1'>698929</p><p i='State' dec='0'>4</p></Data></InverterData></rws_response></string>
所以以下代码对我有用
<?php
date_default_timezone_set('Europe/Zurich');
libxml_use_internal_errors(true);
$urlBase = 'http://localhost/result.xml';
$options=array( CURLOPT_URL => $urlBase,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_FAILONERROR => true );
$CURL=curl_init();
if (!empty($CURL)) {
curl_setopt_array($CURL,$options);
$xmlString = curl_exec($CURL);
if (curl_errno($CURL)) {
$error = "curl_exec error : " . curl_error($CURL);
}
else {
$xml = simplexml_load_string( trim($xmlString) );
$xml2 = simplexml_load_string( trim($xml) );
echo '<pre>';
$lastData = count( $xml2->InverterData->Data ) - 1;
if ( $lastData > 0 ) {
foreach ( $xml2->InverterData->Data[$lastData]->p as $p) {
foreach($p->attributes() as $a => $b) {
echo $a .' = ' . $b . '<br/>';
}
echo $p . '<br/>';
}
}
echo '</pre>';
}
curl_close($CURL);
}
?>
,结果是
i = PAC
dec = 1
3894
i = IAC
dec = 1
17
i = UAC
dec = 1
2295
i = FAC
dec = 2
4999
i = PDC
dec = 0
429
i = IDC
dec = 1
8
i = UDC
dec = 1
5476
i = Temp1
dec = 1
158
i = Temp2
dec = 1
238
i = Energy
dec = 0
0
i = TotalEnergy
dec = 1
698929
i = State
dec = 0
4
希望这可以帮助任何使用此API的人。