I have this issue with this little bitcoin ticker code I'm trying to put on my site bitcoinvalues.net, I used to work pulling the data from Mt.Gox but not anymore cause they shutdown their site. What I want to do now is replacing Mt.Gox with either bitstamp.net/api/ticker/ and blockchain.info/ticker ticker. The codes are below. Can you guys help me with this?

<?php
         //first fetch the current rate from MtGox
         $ch = curl_init('https://mtgox.com/api/0/data/ticker.php');
                 curl_setopt($ch, CURLOPT_REFERER, 'Mozilla/5.0 (compatible; MtGox PHP client; '.php_uname('s').'; PHP/'.phpversion().')');
                 curl_setopt($ch, CURLOPT_USERAGENT, "CakeScript/0.1");
                 curl_setopt($ch, CURLOPT_HEADER, 0);
                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
                 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                 $mtgoxjson = curl_exec($ch);
                 curl_close($ch);

         //decode from an object to array
                 $output_mtgox = json_decode($mtgoxjson);
                 $output_mtgox_1 = get_object_vars($output_mtgox);
                 $mtgox_array = get_object_vars($output_mtgox_1['ticker']);

 ?>
 <br/>
 <br/>
 Last:&nbsp;<?php echo $mtgox_array['last'];   ?><br/>
 High:&nbsp;<?php echo $mtgox_array['high'];   ?><br/>
 Low:&nbsp;&nbsp;<?php echo $mtgox_array['low'];   ?><br/>
 Avg:&nbsp;&nbsp;&nbsp;<?php echo $mtgox_array['avg'];   ?><br/>
 Vol:&nbsp;&nbsp;&nbsp;<?php echo $mtgox_array['vol'];   ?><br/>
Member Avatar for diafol

Here's the data extraction - up to you how you display it:

header('Content-Type: text/html; charset=utf-8');
function extractData($url, $clientName)
{
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_REFERER, 'Mozilla/5.0 (compatible; ' . $clientName. '  PHP client; '.php_uname('s').'; PHP/'.phpversion().')');
    curl_setopt($ch, CURLOPT_USERAGENT, "CakeScript/0.1");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $json = curl_exec($ch);
    curl_close($ch);
    $array = json_decode($json,true);
    return $array;               
}

$url = 'http://www.bitstamp.net/api/ticker/';
$clientName = 'Bitstamp';

$bitstamp = extractData($url,$clientName);

$url = 'http://blockchain.info/ticker';
$clientName =  'Blockchain';

$blockchain = extractData($url,$clientName);
echo "<pre>";
print_r($bitstamp);

print_r($blockchain);
echo "</pre>";

The blockchain gives an array of arrays - a number of different currencies.

Works like a charm. Thank you so much. There is one little issue though. I pulled Bitstamp using

<?php echo $bitstamp['last'];   ?>

but the same is not working for Blockchain. I tried $blockchain and $blockchain_array but neither working. And for Blockchain, how can I add an api key that will allow me to make unlimited request? They have issued me an api key so I can do do. You can see it on the site: http://www.bitcoinvalues.net

Member Avatar for diafol

blockchain returns a different json as I mentioned in my post:

{
  "USD" : {"15m" : 523.29, "last" : 523.29, "buy" : 522.78, "sell" : 523.26,  "symbol" : "$"},
  "CNY" : {"15m" : 3255.7, "last" : 3255.7, "buy" : 3252.53, "sell" : 3255.51,  "symbol" : "¥"},
  "JPY" : {"15m" : 53254.86, "last" : 53254.86, "buy" : 53202.95, "sell" : 53251.8,  "symbol" : "¥"},
  "SGD" : {"15m" : 655.26, "last" : 655.26, "buy" : 654.62, "sell" : 655.22,  "symbol" : "$"},
  "HKD" : {"15m" : 4057.41, "last" : 4057.41, "buy" : 4053.46, "sell" : 4057.18,  "symbol" : "$"},
  "CAD" : {"15m" : 570.3, "last" : 570.3, "buy" : 569.74, "sell" : 570.27,  "symbol" : "$"},
  "NZD" : {"15m" : 611.04, "last" : 611.04, "buy" : 610.44, "sell" : 611,  "symbol" : "$"},
  "AUD" : {"15m" : 567.25, "last" : 567.25, "buy" : 566.7, "sell" : 567.22,  "symbol" : "$"},
  "CLP" : {"15m" : 288972.09, "last" : 288972.09, "buy" : 288690.46, "sell" : 288955.52,  "symbol" : "$"},
  "GBP" : {"15m" : 310.37, "last" : 310.37, "buy" : 310.06, "sell" : 310.35,  "symbol" : "£"},
  "DKK" : {"15m" : 2861.26, "last" : 2861.26, "buy" : 2858.47, "sell" : 2861.09,  "symbol" : "kr"},
  "SEK" : {"15m" : 3449.17, "last" : 3449.17, "buy" : 3445.81, "sell" : 3448.98,  "symbol" : "kr"},
  "ISK" : {"15m" : 58997.81, "last" : 58997.81, "buy" : 58940.31, "sell" : 58994.42,  "symbol" : "kr"},
  "CHF" : {"15m" : 468.17, "last" : 468.17, "buy" : 467.71, "sell" : 468.14,  "symbol" : "CHF"},
  "BRL" : {"15m" : 1158.95, "last" : 1158.95, "buy" : 1157.82, "sell" : 1158.88,  "symbol" : "R$"},
  "EUR" : {"15m" : 383.46, "last" : 383.46, "buy" : 383.09, "sell" : 383.44,  "symbol" : "€"},
  "RUB" : {"15m" : 17963.48, "last" : 17963.48, "buy" : 17945.97, "sell" : 17962.45,  "symbol" : "RUB"},
  "PLN" : {"15m" : 1595.3, "last" : 1595.3, "buy" : 1593.75, "sell" : 1595.21,  "symbol" : "zł"},
  "THB" : {"15m" : 17018, "last" : 17018, "buy" : 17001.42, "sell" : 17017.03,  "symbol" : "฿"},
  "KRW" : {"15m" : 536366.14, "last" : 536366.14, "buy" : 535843.4, "sell" : 536335.39,  "symbol" : "₩"},
  "TWD" : {"15m" : 15787.31, "last" : 15787.31, "buy" : 15771.92, "sell" : 15786.4,  "symbol" : "NT$"}

So decide upon a currency to compare with - let's assume USD:

$blockchain = extractData($url,$clientName)['USD'];

Now...

echo $blockchain['last']; //should give 523.29

As for your KEY, I'd have to look at the documentation, but it should all be there. From what I remember though, is the ticker part of the api? I thought it was separate. The Key (AFAIK) will allow you to buy/sell and change account settings.

I tried $blockchain = extractData($url,$clientName)['USD']; and it broke the site. http://www.bitcoinvalues.net shows a blank page.

As for the KEY, blockchain has this on their site "For an API Code to bypass the request limiter please Request An API Code (https://blockchain.info/api/api_create_code)" The code as I understand it will allow unlimited request from their data.

Member Avatar for diafol

I tried $blockchain = extractData($url,$clientName)['USD'];

What version of PHP are you using? Perhaps it's too old.

Anyway, use this in that case...

$blockChainArray = extractData($url,$clientName);
$blockchain = $blockChainArray['USD'];

and it broke the site. http://www.bitcoinvalues.net shows a blank page.

Tell me you're not updating the live site without testing it locally or on a testing server!

Member Avatar for diafol

Sorry I'm not signing up to anything. A quick look at the API, suggests what I originally thought. The ticker is not part of the api - or if it is, it's not documented (AFAIK).

So does that mean I don't have to worry about request limit?

Member Avatar for diafol

Without delving into the documentation myself, I couldn't really say. Read the API documentation - and if the answer's not there - contact them.

Thank you sir. It's working very well now. Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.