我需要从file to parsing解析xml,但我只能解析本地文件,我如何从url解析文件?
我使用这个PHP代码
<?php
include '/example1.php';
$string = $xmlstr;
$xml = simplexml_load_string($string);
foreach($xml->row->exchangerate[0]->attributes() as $a => $b) {
echo $a,'="',$b,"\"\n";
}
?>
example1.php代码
<?php
$xmlstr = <<<XML
<exchangerates>
<row>
<exchangerate ccy="RUR" base_ccy="UAH" buy="0.33291" sale="0.33291"/>
</row>
<row>
<exchangerate ccy="EUR" base_ccy="UAH" buy="18.60253" sale="18.60253"/>
</row>
<row>
<exchangerate ccy="USD" base_ccy="UAH" buy="14.97306" sale="14.97306"/>
</row>
</exchangerates>
XML;
?>
答案 0 :(得分:0)
如何在加载前下载xml
<?php
$string = file_get_contents("https://www.unicreditbank.cz/web/exchange_rates_xml.php");
?>
像这样
<?php
$string = file_get_contents("https://www.unicreditbank.cz/web/exchange_rates_xml.php");
$xml = simplexml_load_string($string);
foreach($xml->row->exchangerate[0]->attributes() as $a => $b) {
echo $a,'="',$b,"\"\n";
}
?>
答案 1 :(得分:0)
只需输入必要的网址,然后使用您想要使用网址的simplexml_load_file()
:
$url = 'https://api.privatbank.ua/p24api/pubinfo?exchange&coursid=3';
$xml = simplexml_load_file($url);
foreach($xml->row[0]->exchangerate->attributes() as $key => $exchangerate) {
if($key == 'buy' || $key == 'sale') {
$exchangerate = 10 * (float) $exchangerate;
}
echo '<div id="attribute_'.$key.'"></div>','<div id="valuta">'.$exchangerate.'</div><br/>';
}