Author

Topic: btc-e ticker using php (Read 905 times)

member
Activity: 90
Merit: 10
February 07, 2014, 01:54:37 AM
#3
legendary
Activity: 1512
Merit: 1032
February 06, 2014, 11:08:05 PM
#2
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:


When you loaded it just now, it got the prices from bitcoinaverage.com. Here's the code, with some misc commented-out other stuff:

Code:
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(240110);
imagesavealpha($myImagetrue);

$trans_colour imagecolorallocatealpha($myImage000127);
imagefill($myImage00$trans_colour);

$src imagecreatefrompng('callogo.png');
imagecopy($myImage$src0000174120);
$white = ImageColorAllocate($myImage255255255);
$black = ImageColorAllocate($myImage000);
$head = ImageColorAllocate($myImage0064);


Imagestring($myImage455gmdate('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($myImage3525, ("Exchange averages"), $head);
Imagestring($myImage3540, ("USD " $pieces[4]), $black);
Imagestring($myImage3555, ("USD " $pieces[1]), $black);
Imagestring($myImage3570, ("USD " $pieces[2]), $black);
Imagestring($myImage3585, ("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);
 
?>

member
Activity: 90
Merit: 10
February 06, 2014, 09:17:17 PM
#1
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
Jump to: