大家好,有人可以帮我解析下面的xml数据吗?
<?xml version="1.0" encoding="UTF-8"?>
<gesmes:Envelope xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01" xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref">
<gesmes:subject>Reference rates</gesmes:subject>
<gesmes:Sender>
<gesmes:name>European Central Bank</gesmes:name>
</gesmes:Sender>
<Cube>
<Cube time='2014-10-23'>
<Cube currency='USD' rate='1.2669'/>
<Cube currency='JPY' rate='136.42'/>
<Cube currency='BGN' rate='1.9558'/>
<Cube currency='CZK' rate='27.690'/>
<Cube currency='DKK' rate='7.4462'/>
<Cube currency='GBP' rate='0.79040'/>
<Cube currency='HUF' rate='307.40'/>
<Cube currency='LTL' rate='3.4528'/>
<Cube currency='PLN' rate='4.2290'/>
<Cube currency='RON' rate='4.4245'/>
<Cube currency='SEK' rate='9.1869'/>
<Cube currency='CHF' rate='1.2067'/>
<Cube currency='NOK' rate='8.3090'/>
<Cube currency='HRK' rate='7.6733'/>
<Cube currency='RUB' rate='52.6736'/>
<Cube currency='TRY' rate='2.8336'/>
<Cube currency='AUD' rate='1.4404'/>
<Cube currency='BRL' rate='3.1596'/>
<Cube currency='CAD' rate='1.4208'/>
<Cube currency='CNY' rate='7.7542'/>
<Cube currency='HKD' rate='9.8279'/>
<Cube currency='IDR' rate='15257.82'/>
<Cube currency='ILS' rate='4.7737'/>
<Cube currency='INR' rate='77.5153'/>
<Cube currency='KRW' rate='1339.01'/>
<Cube currency='MXN' rate='17.1095'/>
<Cube currency='MYR' rate='4.1440'/>
<Cube currency='NZD' rate='1.6115'/>
<Cube currency='PHP' rate='56.775'/>
<Cube currency='SGD' rate='1.6134'/>
<Cube currency='THB' rate='40.993'/>
<Cube currency='ZAR' rate='13.8557'/>
</Cube>
</Cube>
我正在尝试获取货币=&#39; CZK&#39;以及以下代码的费率
$ch = curl_init();
$cv = curl_version();
$user_agent = "curl ${cv['version']} (${cv['host']}) libcurl/${cv['version']} ${cv['ssl_version']} zlib/${cv['libz_version']} <" . EMAIL_ADDRESS . ">";
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt");
curl_setopt($ch, CURLOPT_ENCODING, "deflate, gzip, identity");
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
curl_setopt($ch, CURLOPT_URL, "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml");
$xml_data = curl_exec($ch);
curl_close($ch);
echo $xml_data;
$parser = simplexml_load_string($xml_data);
echo $parser;
$ xml_data的唯一结果是&#34;参考率欧洲中央银行&#34; 而对于$ parser nothing
为什么$ xml_data的结果仅为&#34;参考率欧洲中央银行&#34;而不是其余的xml文件?
答案 0 :(得分:1)
正如Ian已经指出的那样,$ parser是一个对象,所以你不能简单地回应它。
如果您只是在'CZK'的值之后,那么您可以使用以下代码:
$ch = curl_init();
$cv = curl_version();
$user_agent = "curl ${cv['version']} (${cv['host']}) libcurl/${cv['version']} ${cv['ssl_version']} zlib/${cv['libz_version']} <" . EMAIL_ADDRESS . ">";
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt");
curl_setopt($ch, CURLOPT_ENCODING, "deflate, gzip, identity");
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
curl_setopt($ch, CURLOPT_URL, "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml");
$xml_data = curl_exec($ch);
curl_close($ch);
$parser = simplexml_load_string($xml_data);
// Echo the rate for CZK
echo $parser->Cube->Cube->Cube[3]['rate'];
// Echo the currency
echo $parser->Cube->Cube->Cube[3]['currency'];
显然,上面的代码片段依赖于XML Feed,每次都以相同的顺序返回数据。您应该根据属性选择正确的节点,以便每次都定位正确的元素。
希望有所帮助:)
答案 1 :(得分:0)
根据PHP documentation simplexml_load_string
将XML字符串解释为对象。 Echo不会显示完整的对象,因此您将要使用print_r($ parser)来查看页面上XML文件中的所有数据(尽管使用像xdebug这样的调试器效率更高,并且允许您查看变量中的数据,而无需将它们回显到页面中)。
您将看到每个多维数据集都表示为对象,然后它们的属性是数组。您应该能够轻松地遍历并获取所需的数据。
您可以使用echo '<pre>' . print_r( $parser, true ) . '</pre>';
SimpleXMLElement Object
(
[Cube] => SimpleXMLElement Object
(
[Cube] => SimpleXMLElement Object
(
[@attributes] => Array
(
[time] => 2014-10-23
)
[Cube] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[currency] => USD
[rate] => 1.2669
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[currency] => JPY
[rate] => 136.42
)
)
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[currency] => BGN
[rate] => 1.9558
)
)
[3] => SimpleXMLElement Object
(
[@attributes] => Array
(
[currency] => CZK
[rate] => 27.690
)
)
[4] => SimpleXMLElement Object
(
[@attributes] => Array
(
[currency] => DKK
[rate] => 7.4462
)
)
[5] => SimpleXMLElement Object
(
[@attributes] => Array
(
[currency] => GBP
[rate] => 0.79040
)
)
[6] => SimpleXMLElement Object
(
[@attributes] => Array
(
[currency] => HUF
[rate] => 307.40
)
)
[7] => SimpleXMLElement Object
(
[@attributes] => Array
(
[currency] => LTL
[rate] => 3.4528
)
)
[8] => SimpleXMLElement Object
(
[@attributes] => Array
(
[currency] => PLN
[rate] => 4.2290
)
)
[9] => SimpleXMLElement Object
(
[@attributes] => Array
(
[currency] => RON
[rate] => 4.4245
)
)
[10] => SimpleXMLElement Object
(
[@attributes] => Array
(
[currency] => SEK
[rate] => 9.1869
)
)
[11] => SimpleXMLElement Object
(
[@attributes] => Array
(
[currency] => CHF
[rate] => 1.2067
)
)
[12] => SimpleXMLElement Object
(
[@attributes] => Array
(
[currency] => NOK
[rate] => 8.3090
)
)
[13] => SimpleXMLElement Object
(
[@attributes] => Array
(
[currency] => HRK
[rate] => 7.6733
)
)
[14] => SimpleXMLElement Object
(
[@attributes] => Array
(
[currency] => RUB
[rate] => 52.6736
)
)
[15] => SimpleXMLElement Object
(
[@attributes] => Array
(
[currency] => TRY
[rate] => 2.8336
)
)
[16] => SimpleXMLElement Object
(
[@attributes] => Array
(
[currency] => AUD
[rate] => 1.4404
)
)
[17] => SimpleXMLElement Object
(
[@attributes] => Array
(
[currency] => BRL
[rate] => 3.1596
)
)
[18] => SimpleXMLElement Object
(
[@attributes] => Array
(
[currency] => CAD
[rate] => 1.4208
)
)
[19] => SimpleXMLElement Object
(
[@attributes] => Array
(
[currency] => CNY
[rate] => 7.7542
)
)
[20] => SimpleXMLElement Object
(
[@attributes] => Array
(
[currency] => HKD
[rate] => 9.8279
)
)
[21] => SimpleXMLElement Object
(
[@attributes] => Array
(
[currency] => IDR
[rate] => 15257.82
)
)
[22] => SimpleXMLElement Object
(
[@attributes] => Array
(
[currency] => ILS
[rate] => 4.7737
)
)
[23] => SimpleXMLElement Object
(
[@attributes] => Array
(
[currency] => INR
[rate] => 77.5153
)
)
[24] => SimpleXMLElement Object
(
[@attributes] => Array
(
[currency] => KRW
[rate] => 1339.01
)
)
[25] => SimpleXMLElement Object
(
[@attributes] => Array
(
[currency] => MXN
[rate] => 17.1095
)
)
[26] => SimpleXMLElement Object
(
[@attributes] => Array
(
[currency] => MYR
[rate] => 4.1440
)
)
[27] => SimpleXMLElement Object
(
[@attributes] => Array
(
[currency] => NZD
[rate] => 1.6115
)
)
[28] => SimpleXMLElement Object
(
[@attributes] => Array
(
[currency] => PHP
[rate] => 56.775
)
)
[29] => SimpleXMLElement Object
(
[@attributes] => Array
(
[currency] => SGD
[rate] => 1.6134
)
)
[30] => SimpleXMLElement Object
(
[@attributes] => Array
(
[currency] => THB
[rate] => 40.993
)
)
[31] => SimpleXMLElement Object
(
[@attributes] => Array
(
[currency] => ZAR
[rate] => 13.8557
)
)
)
)
)
)
答案 2 :(得分:0)
我实际上使用了这个银行的xml。我很高兴它也提供了90天的历史xml。我对此的看法不同。我创建了一个包含所有货币作为键的数组,并使它们等于速率。然后我可以使用我的数组来获得如下速率:
$xml=file("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml");
// base rate EUR
$_RATES['EUR']=1;
foreach($xml as $line){
if(preg_match("/currency='([[:alpha:]]+)'/",$line,$currency)){
if(preg_match("/rate='([[:graph:]]+)'/", $line,$rate)){$_RATES[$currency[1]]=$rate[1];}
}
}
现在您的费率为$_RATES["CZK"]
这使您可以轻松地将其兑换为任何货币。例如,如果您想要从USD到CZK的汇率并获得交换价值,您只需:
$converted = ($_RATES[$currency_to]/$_RATES[$currency_from]) * $value;
希望这有助于某人。