Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bitcoin Price Ticker Script

Status
Not open for further replies.

moukieb

Programmer
May 23, 2014
2
US
Folks, I'm trying to fix this little bitcoin price ticker script for my site bitcoinvalues.net but having a hard time. The PHP script is supposed to pull Mt.Gox's latest bitcoin prices but Mt.Gox had to shut down their business. I want to replace Mt.Gox with something like bitstamp.net/api/ticker/ and blockchain.info/ticker...one of those tickers but I cannot get it to work no matter how much I tried. Some help would be helpful. The script is below.

Code:
<?php
         //first fetch the current rate from MtGox
         $ch = curl_init('[URL unfurl="true"]https://mtgox.com/api/0/data/ticker.php');[/URL]
                 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/>

 
the api here looks pretty straightforward:
here is a tiny script I wrote to give you the output as per your mount_gox script. It handles only localbtcUSD trades. You can change the comparison to any other symbol to get alternative information.

Code:
<?php
foreach(json_decode(file_get_contents("[URL unfurl="true"]http://api.bitcoincharts.com/v1/markets.json"))[/URL] as $item)
	if($item->symbol == 'localbtcUSD') break;
printf("<pre>\nLast:\t%s\nHight:\t%s\nLow:\t%s\nAvg:\t%s\nVol:\t%s\n</pre>", $item->latest_trade, $item->high, $item->low, $item->avg, $item->volume);
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top