0

http://btcrate.com/convert?from=btc&to=usd&exch=mtgox&conv=xe&amount=0.01を出力する URL があります{"converted": "1.300000300"}。の値をPHP値に変換したいので、$usd = 1.300000300;.


私は次のことを試しましたが、変換された値が欲しいだけですが、文字列全体を出力するだけです。

file_get_contents("http://btcrate.com/convert?from=btc&to=usd&exch=mtgox&conv=xe&amount=0.01");
4

2 に答える 2

2

返されるデータはJSON形式であるため、 をデコードしてJSONから値を取得するだけです。converted

$data = file_get_contents("http://btcrate.com/convert?from=btc&to=usd&exch=mtgox&conv=xe&amount=0.01");

$obj = json_decode($data );
$converted = $obj->{'converted'};

echo $converted;

PHP での JSON の使用について詳しくは、こちらをご覧ください。

于 2013-04-28T05:20:29.973 に答える
0

もう 1 つの可能性は、配列を使用することです。

$string = file_get_contents("http://btcrate.com/convert?from=btc&to=usd&exch=mtgox&conv=xe&amount=0.01");

$result = json_decode($string, true);
$converted = $result['converted'];

echo $converted;
于 2013-04-28T05:29:08.480 に答える