0

我对 xml 和 php 还很陌生,我无法仅从外部 url http://www.xmlcharts.com/cache/precious-metals.php中的 xml 提要中以美元货币显示金属

<prices>
<currency access="aud">
<price access="gold">47.434995697675</price>
<price access="palladium">26.322725436389</price>
<price access="platinum">51.924285690151</price>
<price access="silver">0.71125071751027</price>
</currency>
<currency access="brl">
<price access="gold">96.721773294575</price>
<price access="palladium">53.673045495365</price>
<price access="platinum">105.87560755807</price>
<price access="silver">1.4502674585044</price>
</currency>
<currency access="cad">
<price access="gold">43.634985906498</price>
<price access="palladium">24.214016182439</price>
<price access="platinum">47.764639607752</price>
<price access="silver">0.65427253819838</price>
</currency>
<currency access="chf">
<price access="gold">39.490112355758</price>
<price access="palladium">21.913934421285</price>
<price access="platinum">43.227491554238</price>
<price access="silver">0.59212339612314</price>
</currency>
<currency access="cny">
<price access="gold">259.21346527132</price>
<price access="palladium">143.84326962395</price>
<price access="platinum">283.74565713604</price>
<price access="silver">3.8867034865481</price>
</currency>
<currency access="eur">
<price access="gold">31.961694805017</price>
<price access="palladium">17.736249460129</price>
<price access="platinum">34.986577900722</price>
<price access="silver">0.47924065404789</price>
</currency>
<currency access="gbp">
<price access="gold">27.893018510833</price>
<price access="palladium">15.4784512374</price>
<price access="platinum">30.53283847959</price>
<price access="silver">0.41823403033067</price>
</currency>
<currency access="inr">
<price access="gold">2558.8559365892</price>
<price access="palladium">1419.9656025986</price>
<price access="platinum">2801.0283280769</price>
<price access="silver">38.368046505244</price>
</currency>
<currency access="jpy">
<price access="gold">4196.440446</price>
<price access="palladium">2328.6973688</price>
<price access="platinum">4593.5952854</price>
<price access="silver">62.9223474</price>
</currency>
<currency access="mxn">
<price access="gold">540.37670151162</price>
<price access="palladium">299.86695132785</price>
<price access="platinum">591.51843100021</price>
<price access="silver">8.1025266477426</price>
</currency>
<currency access="rub">
<price access="gold">1399.5182262463</price>
<price access="palladium">776.62353439423</price>
<price access="platinum">1531.9698703324</price>
<price access="silver">20.984682889624</price>
</currency>
<currency access="usd">
<price access="gold">42.2943</price>
<price access="palladium">23.47004</price>
<price access="platinum">46.29707</price>
<price access="silver">0.63417</price>
</currency>
<currency access="zar">
<price access="gold">421.22164931813</price>
<price access="palladium">233.74518453698</price>
<price access="platinum">461.08643916549</price>
<price access="silver">6.315889690764</price>
</currency>
</prices>

我已经使用 PHP 来加载这个,这里是代码。

    <?php header('Content-Type: text/plain'); 
$content = file_get_contents('http://www.xmlcharts.com/cache/precious-metals.php?format=json'); 
if ($content === false) die('Something went wrong.'); 
foreach (json_decode($content, true) as $currency["11"] => $arr) 
    { 
foreach ($arr as $commodity => $price) 
    { 
        print $commodity.' '.round($price, 2); 
    }  
    } 
?>

我的目标输出是

金 42.29 钯 23.47 铂 46.3 银 0.63

4

3 回答 3

4

像这样!

$content = file_get_contents('http://www.xmlcharts.com/cache/precious-metals.php?format=json'); 
$metals = json_decode($content);
echo 'gold '.$metals->usd->gold;
echo 'palladium '.$metals->usd->palladium;
echo 'platinum '.$metals->usd->platinum;
echo 'silver '.$metals->usd->silver;
于 2013-08-01T20:30:49.490 回答
0
$data = file_get_contents('http://www.xmlcharts.com/cache/precious-metals.php?format=json');
$array = json_decode($data, true);
foreach ($array['usd'] as $metal => $price) {
    echo $metal . ' ' . round($price, 2) . ' ';
}
于 2013-08-01T20:34:58.437 回答
0

仅显示 usd:

$data = json_decode($content, true);
foreach($data['usd'] as $commodity => $price) 
{
    print $commodity.' '.round($price, 2); 
}
于 2013-08-01T20:35:30.583 回答