0

私はxmlとphpにまったく慣れていないので、外部URLのxmlフィードからのみ米ドル通貨で金属を表示する方法に問題があり ます http://www.xmlcharts.com/cache/precious-metals.php

<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 に答える