Title: btc-e ticker using php
Post by: gigamike on February 07, 2014, 01:17:17 AM
Guys,
I hope you can help me. I setup a btc-e ticker using php. What i did was i have a cron that runs every minute and cURL on https://btc-e.com/api/2/btc_usd/ticker and save its data. And on my site i have an ajax with setInterval for 5 seconds to display latest price.
Is there a better way for real time. I know websocket or streaming but i cant get the logic. Do you have any sample using php?
Thanks,
Mike
Title: Re: btc-e ticker using php
Post by: deepceleron on February 07, 2014, 03:08:05 AM
Are you gathering data for a database to make charts for your own sites, or do you just want to re-serve the current price to clients. If you just want to get the price from the site every time a user loads the ticker page or image, that actually might be less queries to the API overall if you are low traffic (if you are going to have less than 86K page loads a day). Here's an image: http://we.lovebitco.in/img/cal.phpWhen you loaded it just now, it got the prices from bitcoinaverage.com. Here's the code, with some misc commented-out other stuff: <?php header ('Content-Type: image/png'); $block = file_get_contents('http://blockexplorer.com/q/getblockcount'); $dif = file_get_contents('http://blockexplorer.com/q/getdifficulty'); $blksiz = file_get_contents('http://blockexplorer.com/q/avgblocksize/144');
$priceget = file_get_contents('http://api.bitcoinaverage.com/ticker/USD'); $pieces = explode("\n", $priceget);
#$pieces[1] = trim($pieces[1]) #$pieces[4] = trim($pieces[4]) #$cutlist = array(" ", ",")
#$avg = explode(":", $pieces[1]); #$avgname = substr(trim($avg[0]), 1, -1)
#$avgval = trim($avg[1], $cutlist)
#$prc = explode(":", $pieces[4]); #$prcname = substr(trim($prc[0]), 1, -1) #$prcval = trim($prc[1], $cutlist)
$myImage = imagecreatetruecolor(240, 110); imagesavealpha($myImage, true);
$trans_colour = imagecolorallocatealpha($myImage, 0, 0, 0, 127); imagefill($myImage, 0, 0, $trans_colour);
$src = imagecreatefrompng('callogo.png'); imagecopy($myImage, $src, 0, 0, 0, 0, 174, 120); $white = ImageColorAllocate($myImage, 255, 255, 255); $black = ImageColorAllocate($myImage, 0, 0, 0); $head = ImageColorAllocate($myImage, 0, 0, 64);
Imagestring($myImage, 4, 5, 5, gmdate('F j, Y, g:i a',is_null($ts)?time():$ts) . " UTC", $head); #Imagestring($myImage, 2, 5, 25, ("Current block: " . $block), $black); #Imagestring($myImage, 2, 5, 40, ("Current difficulty: " . $dif), $black); #Imagestring($myImage, 2, 5, 55, ("average blocksize:" . round($blksiz/1000) . "kB"), $black); Imagestring($myImage, 3, 5, 25, ("Exchange averages"), $head); Imagestring($myImage, 3, 5, 40, ("USD " . $pieces[4]), $black); Imagestring($myImage, 3, 5, 55, ("USD " . $pieces[1]), $black); Imagestring($myImage, 3, 5, 70, ("USD " . $pieces[2]), $black); Imagestring($myImage, 3, 5, 85, ("USD " . $pieces[3]), $black);
#Imagestring($myImage, 3, 5, 70, ("USD " . $avgname . ": " . $avgval ), $black); #Imagestring($myImage, 3, 5, 85, ("USD " . $prcname . ": " . $prcval ), $black);
$blksiz = file_get_contents('http://blockexplorer.com/q/avgblocksize/144');
header("Content-type: image/png"); imagepng($myImage);
Imagedestroy($myImage); ?>
Title: Re: btc-e ticker using php
Post by: gigamike on February 07, 2014, 05:54:37 AM
Are you gathering data for a database to make charts for your own sites, or do you just want to re-serve the current price to clients. If you just want to get the price from the site every time a user loads the ticker page or image, that actually might be less queries to the API overall if you are low traffic (if you are going to have less than 86K page loads a day). Here's an image: http://we.lovebitco.in/img/cal.phpWhen you loaded it just now, it got the prices from bitcoinaverage.com. Here's the code, with some misc commented-out other stuff: <?php header ('Content-Type: image/png'); $block = file_get_contents('http://blockexplorer.com/q/getblockcount'); $dif = file_get_contents('http://blockexplorer.com/q/getdifficulty'); $blksiz = file_get_contents('http://blockexplorer.com/q/avgblocksize/144');
$priceget = file_get_contents('http://api.bitcoinaverage.com/ticker/USD'); $pieces = explode("\n", $priceget);
#$pieces[1] = trim($pieces[1]) #$pieces[4] = trim($pieces[4]) #$cutlist = array(" ", ",")
#$avg = explode(":", $pieces[1]); #$avgname = substr(trim($avg[0]), 1, -1)
#$avgval = trim($avg[1], $cutlist)
#$prc = explode(":", $pieces[4]); #$prcname = substr(trim($prc[0]), 1, -1) #$prcval = trim($prc[1], $cutlist)
$myImage = imagecreatetruecolor(240, 110); imagesavealpha($myImage, true);
$trans_colour = imagecolorallocatealpha($myImage, 0, 0, 0, 127); imagefill($myImage, 0, 0, $trans_colour);
$src = imagecreatefrompng('callogo.png'); imagecopy($myImage, $src, 0, 0, 0, 0, 174, 120); $white = ImageColorAllocate($myImage, 255, 255, 255); $black = ImageColorAllocate($myImage, 0, 0, 0); $head = ImageColorAllocate($myImage, 0, 0, 64);
Imagestring($myImage, 4, 5, 5, gmdate('F j, Y, g:i a',is_null($ts)?time():$ts) . " UTC", $head); #Imagestring($myImage, 2, 5, 25, ("Current block: " . $block), $black); #Imagestring($myImage, 2, 5, 40, ("Current difficulty: " . $dif), $black); #Imagestring($myImage, 2, 5, 55, ("average blocksize:" . round($blksiz/1000) . "kB"), $black); Imagestring($myImage, 3, 5, 25, ("Exchange averages"), $head); Imagestring($myImage, 3, 5, 40, ("USD " . $pieces[4]), $black); Imagestring($myImage, 3, 5, 55, ("USD " . $pieces[1]), $black); Imagestring($myImage, 3, 5, 70, ("USD " . $pieces[2]), $black); Imagestring($myImage, 3, 5, 85, ("USD " . $pieces[3]), $black);
#Imagestring($myImage, 3, 5, 70, ("USD " . $avgname . ": " . $avgval ), $black); #Imagestring($myImage, 3, 5, 85, ("USD " . $prcname . ": " . $prcval ), $black);
$blksiz = file_get_contents('http://blockexplorer.com/q/avgblocksize/144');
header("Content-type: image/png"); imagepng($myImage);
Imagedestroy($myImage); ?>
Hi, thanks for reply, im looking for gathering data for charts not only as display of current price. thanks again, Mike
|