0

I make a JSON call to

https://blockchain.info/tobtc?currency=USD&value=1

This is a free api with a request limit.

I have just received my api_code via email. This is blockchain.info's example of how to use it

Once approved the API Code can be passed to all requests in the "api_code" parameter e.g. https://blockchain.info?api_code=$your_code

I am guessing that they mean with PHP (because of the dollar sign) as when I type into my address bar

https://blockchain.info/tobtc?api_code=000my0-api00-code-0000-000000000&currency=USD&value=1

The data does not any look different when I use the api code with the dollar

https://blockchain.info/tobtc?api_code=$000my0-api00-code-0000-000000000&currency=USD&value=1

Though I am unsure of how to tell what is the correct way as I can access this free api with and without an api code (with a request limit). How do I tell if I am adding it correctly?

2

The first one is correct.
https://blockchain.info/tobtc?api_code=000my0-api00-code-0000-000000000&currency=USD&value=1

However, the particular call you are using (/tobtc) is public and thus needs no api_code attached.

https://blockchain.info/tobtc?currency=USD&value=1

| improve this answer | |
0

You should always use api code, even if it's not required. Otherwise if you generate too much traffic, block chain will ban your ip for a day. You will not be able to use web wallet.

| improve this answer | |
0
function convert_amount_to_btc($currency_from,$amount,$api_code) {

    $url = "https://blockchain.info/tobtc?api_code=".$api_code."&currency=".$currency_from."&value=".$amount;
    try 
    {
        $ch = curl_init();
        curl_setopt ($ch, CURLOPT_URL, $url);
        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)");
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 20);
        curl_setopt ($ch, CURLOPT_TIMEOUT, 20);
        $rawdata = curl_exec($ch);
        curl_close($ch);
        if(is_numeric($rawdata)){
            return array('status' => TRUE,'response' => $rawdata);
        }else{
            return array('status' => FALSE,'response' => $rawdata);
        }
    } 
    catch (Exception $e){
        return array('status' => FALSE,'response' => $e->Message());
    }
}

$app_api_key = "your-blockchain-api-key";//Your Own APIKey received from Blockchain

//BELOW AMOUNT IS CONVERTED FROM USD TO BTC 
$app_currency = "USD"; //App Local Currency - please always refer to the blockain docs for supported currencies

$usd_amount = "28";//USD
$convert = convert_amount_to_btc($app_currency,$usd_amount,$app_api_key);
//the response from convert can either be TRUE/FALSE | 
// $convert['status'] (True means the amount was converted, False meaning there was an error/issue 'reason like not a supported currency or your call server IP being blocked') 
// $convert['response'] - carries amount convert - on error carries blockchain call reponse message
if((bool)$convert['status']){
    $amount = $convert['response'];//Amount converted from fiat currency to BTC
}else{
    //here for error purposes it up to your App what to do
    //your can display a message or ask the user to try again
}
| improve this answer | |
  • I hope this function and usability above helps a few people, converting from the local currency to bitcoin – gabisajr Sep 17 '17 at 10:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.