0

I have a url, http://btcrate.com/convert?from=btc&to=usd&exch=mtgox&conv=xe&amount=0.01, which outputs {"converted": "1.300000300"}. I want to get the value of converted into a PHP value, so $usd = 1.300000300;.


I have tried the following, but it just outputs the whole string, although I just want the value of converted.

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

2 Answers 2

2

The data being returned is in JSON format, so you can decode the JSON and then simply retrieve the value of 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;

Learn more about using JSON in PHP here

Sign up to request clarification or add additional context in comments.

Comments

0

Another possibility is to use arrays:

$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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.