Bitcoin Forum
November 02, 2024, 04:27:58 PM *
News: Latest Bitcoin Core release: 28.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: btc-e ticker using php  (Read 926 times)
gigamike (OP)
Member
**
Offline Offline

Activity: 90
Merit: 10


View Profile
February 07, 2014, 01:17:17 AM
 #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
deepceleron
Legendary
*
Offline Offline

Activity: 1512
Merit: 1036



View Profile WWW
February 07, 2014, 03:08:05 AM
 #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:
<?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(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);
 
?>

gigamike (OP)
Member
**
Offline Offline

Activity: 90
Merit: 10


View Profile
February 07, 2014, 05:54:37 AM
 #3

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:
<?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(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);
 
?>


Hi,

thanks for reply, im looking for gathering data for charts not only as display of current price.

thanks again,

Mike
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!