Search code examples
phpapicurrencybitcoin

bitcoin to currency converter and it's reverse in php


I am using Bitcoin Currency Converter in PHP.So that I can convert Bitcoin to any currency (say USD) and Any Currency (say USD) to Bitcoins.

Is there any api to do it ?? Till now I am using this url to convert any currency to Bitcoins https://blockchain.info/tobtc?currency=USD&value=83.652

but how to do it's reverse (convert from bitcoins to currency).Also I don't know whether this result is correct or not.


Solution

  • I previously used the MtGox API to achieve this available as HTTP or streaming web socket

    EDIT:

    Ok, you can get the value by doing this:

    //set currency
    $currency = 'USD';
    //get json response
    $return = file_get_contents('http://data.mtgox.com/api/1/BTC'.$currency.'/ticker_fast');
    //decode it (into an array rather than object [using 'true' parameter])
    $info = json_decode($return, true);
    //access the dollar value
    $value = $info['return']['last_local']['value'];
    

    It's a public API so you don't need to authenticate, you just get the contents of the page which is in JSON format and decode it into an array and access the value element.

    You now know that 1 BTC = 90.20505 dollars - so therefore 1 dollar is equal to 0.0110858 Bitcoins. (1 / 90.20505).

    You can do this with any currency.

    Voila

    UPDATE:

    For new readers of this question, this solution no longer works based on MtGox going under see Nathan's working solution below.