Bitcoin Forum
November 12, 2024, 03:20:59 AM *
News: Latest Bitcoin Core release: 28.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Bitcoin Price Ticker Fix - PLEASE HELP!  (Read 905 times)
JessyMatt (OP)
Full Member
***
Offline Offline

Activity: 191
Merit: 100



View Profile
May 22, 2014, 12:46:45 AM
 #1

I'm using this bitcoin price ticker that is using Mt.Gox but they're no longer in service unfortunately. I'm trying to replace Mt.Gox with blockchain or something similar. Can anyone please help me fix this?

Code:
<?php
        
//first fetch the current rate from MtGox
        
$ch curl_init('https://mtgox.com/api/0/data/ticker.php');
                
curl_setopt($chCURLOPT_REFERER'Mozilla/5.0 (compatible; MtGox PHP client; '.php_uname('s').'; PHP/'.phpversion().')');
                
curl_setopt($chCURLOPT_USERAGENT"CakeScript/0.1");
                
curl_setopt($chCURLOPT_HEADER0);
                
curl_setopt($chCURLOPT_RETURNTRANSFER1);
                
curl_setopt($chCURLOPT_SSL_VERIFYHOSTfalse);
                
curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse);
                
$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/>

DISCIPLINA — The First Blockchain For HR & Education
From core developers of Cardano, PoS minting, unique Web Of Trust & Privacy algorithms. Be the first, join us!
  WEBSITE  TELEGRAM  ANN  BOUNTY  LINKEDIN  WHITEPAPER  Referral Program 5%
cryptomatt
Member
**
Offline Offline

Activity: 85
Merit: 10


View Profile WWW
May 22, 2014, 03:20:28 AM
 #2

First off, switch the URL to https://www.bitstamp.net/api/ticker/ and see what it reads
JessyMatt (OP)
Full Member
***
Offline Offline

Activity: 191
Merit: 100



View Profile
May 22, 2014, 08:06:49 AM
 #3

First off, switch the URL to https://www.bitstamp.net/api/ticker/ and see what it reads

Thanks for the idea but it doesn't work. I tried https://blockchain.info/ticker too and that didn't work.

DISCIPLINA — The First Blockchain For HR & Education
From core developers of Cardano, PoS minting, unique Web Of Trust & Privacy algorithms. Be the first, join us!
  WEBSITE  TELEGRAM  ANN  BOUNTY  LINKEDIN  WHITEPAPER  Referral Program 5%
JessyMatt (OP)
Full Member
***
Offline Offline

Activity: 191
Merit: 100



View Profile
May 22, 2014, 09:26:14 PM
 #4

Can anyone help please

DISCIPLINA — The First Blockchain For HR & Education
From core developers of Cardano, PoS minting, unique Web Of Trust & Privacy algorithms. Be the first, join us!
  WEBSITE  TELEGRAM  ANN  BOUNTY  LINKEDIN  WHITEPAPER  Referral Program 5%
colin012
Member
**
Offline Offline

Activity: 87
Merit: 10


View Profile
May 22, 2014, 11:48:33 PM
 #5

Can anyone help please

You are getting a bad result because you are not using the Bitstamp PHP api. Copy and paste the following code into a new php file:
Code:
<?php

/**
* @package Bitstamp API
* @author https://bxmediaus.com - BX MEDIA - PHP + Bitcoin. We are ready to work on your next bitcoin project. Only high quality coding. https://bxmediaus.com
* @version 0.1
* @access public
* @license http://www.opensource.org/licenses/LGPL-3.0
*/

class Bitstamp
{
private 
$key;
private 
$secret;
private 
$client_id;
public 
$redeemd// Redeemed code information
public $withdrew// Withdrawal information
public $info// Result from getInfo()
public $ticker// Current ticker (getTicker())
public $eurusd// Current eur/usd
/**
* Bitstamp::__construct()
* Sets required key and secret to allow the script to function
* @param Bitstamp API Key $key
* @param Bitstamp Secret $secret
* @return
*/
public function __construct($key$secret$client_id)
{
if (isset(
$secret) && isset($key) && isset($client_id))
{
$this->key $key;
$this->secret $secret;
$this->client_id $client_id;
} else
die(
"NO KEY/SECRET/CLIENT ID");
}
/**
* Bitstamp::bitstamp_query()
*
* @param API Path $path
* @param POST Data $req
* @return Array containing data returned from the API path
*/
public function bitstamp_query($path, array $req = array(), $verb 'post')
{
// API settings
$key $this->key;

// generate a nonce as microtime, with as-string handling to avoid problems with 32bits systems
$mt explode(' 'microtime());
$req['nonce'] = $mt[1] . substr($mt[0], 26);
$req['key'] = $key;
$req['signature'] = $this->get_signature($req['nonce']);


// generate the POST data string
        
$post_data http_build_query($req'''&');

// any extra headers
$headers = array();

// our curl handle (initialize if required)
static $ch null;
if (
is_null($ch))
{
$ch curl_init();
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
curl_setopt($chCURLOPT_USERAGENT,
'Mozilla/4.0 (compatible; MtGox PHP Client; ' php_uname('s') . '; PHP/' .
phpversion() . ')');
}
curl_setopt($chCURLOPT_URL'https://www.bitstamp.net/api/' $path .'/');
curl_setopt($chCURLOPT_SSL_VERIFYPEER1); // man-in-the-middle defense by verifying ssl cert.
curl_setopt($chCURLOPT_SSL_VERIFYHOST2); // man-in-the-middle defense by verifying ssl cert.
        
if ($verb == 'post')
        {
            
curl_setopt($chCURLOPT_POSTFIELDS$post_data);
        }
curl_setopt($chCURLOPT_HTTPHEADER$headers);

// run the query
$res curl_exec($ch);
if (
$res === false)
throw new \
Exception('Could not get reply: ' curl_error($ch));
$dec json_decode($restrue);
if (
is_null($dec))
throw new \
Exception('Invalid data received, please make sure connection is working and requested API exists');
return 
$dec;
}

/**
* Bitstamp::ticker()
* Returns current ticker from Bitstamp
* @return $ticker
*/
function ticker() {
$ticker $this->bitstamp_query('ticker', array(), 'get');
$this->ticker $ticker// Another variable to contain it.
return $ticker;
}

/**
* Bitstamp::eurusd()
* Returns current EUR/USD rate from Bitstamp
* @return $eurusd
*/
function eurusd() {
$eurusd $this->bitstamp_query('eur_usd', array(), 'get');
$this->eurusd $eurusd// Another variable to contain it.
return $eurusd;
}

/**
* Bitstamp::buyBTC()
*
* @param float $amount
* @param float $price
*/
function buyBTC($amount$price=NULL){
if(
is_null($price)){
if (!isset(
$this->ticker))
$this->ticker();

$price $this->ticker['ask'];
}

return 
$this->bitstamp_query('buy', array('amount' => $amount'price' => $price));
}

/**
* Bitstamp::sellBTC()
*
* @param float $amount
* @param float $price
*/
function sellBTC($amount$price=NULL){
if(
is_null($price)){
if (!isset(
$this->ticker))
$this->ticker();

$price $this->ticker['bid'];
}

return 
$this->bitstamp_query('sell', array('amount' => $amount'price' => $price));
}

/**
* Bitstamp::transactions()
*
* @param string $time
*/
function transactions($time='hour'){
return 
$this->bitstamp_query('transactions', array('time' => $time), 'get');
}

/**
* Bitstamp::orderBook()
*
* @param int $group
*/
function orderBook($group=1){
return 
$this->bitstamp_query('order_book', array('group' => $group), 'get');
}

/**
* Bitstamp::openOrders()
* List of open orders
*/
function openOrders(){
return 
$this->bitstamp_query('open_orders');
}

/**
* Bitstamp::cancelOrder()
*
* @param int $id
*/
function cancelOrder($id=NULL){
if(
is_null($id))
throw new 
Exception('Order id is undefined');
return 
$this->bitstamp_query('cancel_order', array('id' => $id));
}

/**
* Bitstamp::balance()
*
*/
function balance(){
$balance $this->bitstamp_query('balance');
$this->balance $balance// Another variable to contain it.
return $balance;
}


/**
* Bitstamp::unconfirmedbtc()
*
*/
function unconfirmedbtc(){
$unconfirmedbtc $this->bitstamp_query('unconfirmed_btc');
$this->unconfirmedbtc $unconfirmedbtc// Another variable to contain it.
return $unconfirmedbtc;
}

/**
* Bitstamp::bitcoindepositaddress()
*
*/
function bitcoindepositaddress(){
$bitcoindepositaddress $this->bitstamp_query('bitcoin_deposit_address');
$this->bitcoindepositaddress $bitcoindepositaddress// Another variable to contain it.
return $bitcoindepositaddress;
}

/**
* Bitstamp::get_signature()
* Compute bitstamp signature
* @param float $nonce
*/
private function get_signature($nonce)
{

$message $nonce.$this->client_id.$this->key;

return 
strtoupper(hash_hmac('sha256'$message$this->secret));

}
}

Save it in the same directory as your other code as "bitstamp.php". Now change your other code to the following:
Code:
require_once('bitstamp.php');
$KEY = "YOUR KEY";
$SECRET = "YOUR SECRET";
$CLIENT_ID = "YOUR CLIENT ID";

$bs = new Bitstamp($KEY,$SECRET,$CLIENT_ID);

print_r($bs->ticker());

Then create an account on bitstamp, verify your account, then on their website, go to Security then API access. Copy and past the key, secret, and client id they provide you with into the matching variables in the code I just gave you. That should do the trick.

NXT Organization Marketing
BitCoinDream
Legendary
*
Offline Offline

Activity: 2394
Merit: 1216

The revolution will be digital


View Profile
May 23, 2014, 04:47:57 PM
 #6

I'm using this bitcoin price ticker that is using Mt.Gox but they're no longer in service unfortunately. I'm trying to replace Mt.Gox with blockchain or something similar. Can anyone please help me fix this?

Code:
<?php
        
//first fetch the current rate from MtGox
        
$ch curl_init('https://mtgox.com/api/0/data/ticker.php');
                
curl_setopt($chCURLOPT_REFERER'Mozilla/5.0 (compatible; MtGox PHP client; '.php_uname('s').'; PHP/'.phpversion().')');
                
curl_setopt($chCURLOPT_USERAGENT"CakeScript/0.1");
                
curl_setopt($chCURLOPT_HEADER0);
                
curl_setopt($chCURLOPT_RETURNTRANSFER1);
                
curl_setopt($chCURLOPT_SSL_VERIFYHOSTfalse);
                
curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse);
                
$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/>

Is not it possible to integrate the coindesk ticker ? Probably the following may help...

http://www.coindesk.com/api/

colin012
Member
**
Offline Offline

Activity: 87
Merit: 10


View Profile
May 23, 2014, 05:13:14 PM
 #7

I'm using this bitcoin price ticker that is using Mt.Gox but they're no longer in service unfortunately. I'm trying to replace Mt.Gox with blockchain or something similar. Can anyone please help me fix this?

Code:
<?php
        
//first fetch the current rate from MtGox
        
$ch curl_init('https://mtgox.com/api/0/data/ticker.php');
                
curl_setopt($chCURLOPT_REFERER'Mozilla/5.0 (compatible; MtGox PHP client; '.php_uname('s').'; PHP/'.phpversion().')');
                
curl_setopt($chCURLOPT_USERAGENT"CakeScript/0.1");
                
curl_setopt($chCURLOPT_HEADER0);
                
curl_setopt($chCURLOPT_RETURNTRANSFER1);
                
curl_setopt($chCURLOPT_SSL_VERIFYHOSTfalse);
                
curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse);
                
$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/>

Is not it possible to integrate the coindesk ticker ? Probably the following may help...

http://www.coindesk.com/api/

Yeah, you could use that instead. You might not have to go through bitstamp's painful verification process. Bitstamp makes you do that because you can use their api to buy and sell BTC so it needs to be extra secure.

OH! I just realized something! Try changing the code to this:

Code:
<?php
        
//first fetch the current rate from MtGox
        
$ch curl_init('https://www.bitstamp.net/api/ticker/');
                
curl_setopt($chCURLOPT_REFERER'Mozilla/5.0 (compatible; MtGox PHP client; '.php_uname('s').'; PHP/'.phpversion().')');
                
curl_setopt($chCURLOPT_USERAGENT"CakeScript/0.1");
                
curl_setopt($chCURLOPT_HEADER0);
                
curl_setopt($chCURLOPT_RETURNTRANSFER1);
                
curl_setopt($chCURLOPT_SSL_VERIFYHOSTfalse);
                
curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse);
                
$mtgoxjson curl_exec($ch);
                
curl_close($ch);
               
        
//decode from an object to array
                
$output_mtgox json_decode($mtgoxjson);
                
$output_array get_object_vars($output_mtgox);
 
?>

<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/>
Volume:&nbsp;&nbsp;&nbsp;<?php echo $mtgox_array['volume'];   ?><br/>

I think the problem you had when you changed the ticker was that the array was built differently so you had to access it differently. See if the above code works. It will be a lot simpler than going through bitstamp's whole verification process.

NXT Organization Marketing
JessyMatt (OP)
Full Member
***
Offline Offline

Activity: 191
Merit: 100



View Profile
May 24, 2014, 04:30:18 PM
 #8

Can anyone help please

You are getting a bad result because you are not using the Bitstamp PHP api. Copy and paste the following code into a new php file:
Code:
<?php

/**
* @package Bitstamp API
* @author https://bxmediaus.com - BX MEDIA - PHP + Bitcoin. We are ready to work on your next bitcoin project. Only high quality coding. https://bxmediaus.com
* @version 0.1
* @access public
* @license http://www.opensource.org/licenses/LGPL-3.0
*/

class Bitstamp
{
private 
$key;
private 
$secret;
private 
$client_id;
public 
$redeemd// Redeemed code information
public $withdrew// Withdrawal information
public $info// Result from getInfo()
public $ticker// Current ticker (getTicker())
public $eurusd// Current eur/usd
/**
* Bitstamp::__construct()
* Sets required key and secret to allow the script to function
* @param Bitstamp API Key $key
* @param Bitstamp Secret $secret
* @return
*/
public function __construct($key$secret$client_id)
{
if (isset(
$secret) && isset($key) && isset($client_id))
{
$this->key $key;
$this->secret $secret;
$this->client_id $client_id;
} else
die(
"NO KEY/SECRET/CLIENT ID");
}
/**
* Bitstamp::bitstamp_query()
*
* @param API Path $path
* @param POST Data $req
* @return Array containing data returned from the API path
*/
public function bitstamp_query($path, array $req = array(), $verb 'post')
{
// API settings
$key $this->key;

// generate a nonce as microtime, with as-string handling to avoid problems with 32bits systems
$mt explode(' 'microtime());
$req['nonce'] = $mt[1] . substr($mt[0], 26);
$req['key'] = $key;
$req['signature'] = $this->get_signature($req['nonce']);


// generate the POST data string
        
$post_data http_build_query($req'''&');

// any extra headers
$headers = array();

// our curl handle (initialize if required)
static $ch null;
if (
is_null($ch))
{
$ch curl_init();
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
curl_setopt($chCURLOPT_USERAGENT,
'Mozilla/4.0 (compatible; MtGox PHP Client; ' php_uname('s') . '; PHP/' .
phpversion() . ')');
}
curl_setopt($chCURLOPT_URL'https://www.bitstamp.net/api/' $path .'/');
curl_setopt($chCURLOPT_SSL_VERIFYPEER1); // man-in-the-middle defense by verifying ssl cert.
curl_setopt($chCURLOPT_SSL_VERIFYHOST2); // man-in-the-middle defense by verifying ssl cert.
        
if ($verb == 'post')
        {
            
curl_setopt($chCURLOPT_POSTFIELDS$post_data);
        }
curl_setopt($chCURLOPT_HTTPHEADER$headers);

// run the query
$res curl_exec($ch);
if (
$res === false)
throw new \
Exception('Could not get reply: ' curl_error($ch));
$dec json_decode($restrue);
if (
is_null($dec))
throw new \
Exception('Invalid data received, please make sure connection is working and requested API exists');
return 
$dec;
}

/**
* Bitstamp::ticker()
* Returns current ticker from Bitstamp
* @return $ticker
*/
function ticker() {
$ticker $this->bitstamp_query('ticker', array(), 'get');
$this->ticker $ticker// Another variable to contain it.
return $ticker;
}

/**
* Bitstamp::eurusd()
* Returns current EUR/USD rate from Bitstamp
* @return $eurusd
*/
function eurusd() {
$eurusd $this->bitstamp_query('eur_usd', array(), 'get');
$this->eurusd $eurusd// Another variable to contain it.
return $eurusd;
}

/**
* Bitstamp::buyBTC()
*
* @param float $amount
* @param float $price
*/
function buyBTC($amount$price=NULL){
if(
is_null($price)){
if (!isset(
$this->ticker))
$this->ticker();

$price $this->ticker['ask'];
}

return 
$this->bitstamp_query('buy', array('amount' => $amount'price' => $price));
}

/**
* Bitstamp::sellBTC()
*
* @param float $amount
* @param float $price
*/
function sellBTC($amount$price=NULL){
if(
is_null($price)){
if (!isset(
$this->ticker))
$this->ticker();

$price $this->ticker['bid'];
}

return 
$this->bitstamp_query('sell', array('amount' => $amount'price' => $price));
}

/**
* Bitstamp::transactions()
*
* @param string $time
*/
function transactions($time='hour'){
return 
$this->bitstamp_query('transactions', array('time' => $time), 'get');
}

/**
* Bitstamp::orderBook()
*
* @param int $group
*/
function orderBook($group=1){
return 
$this->bitstamp_query('order_book', array('group' => $group), 'get');
}

/**
* Bitstamp::openOrders()
* List of open orders
*/
function openOrders(){
return 
$this->bitstamp_query('open_orders');
}

/**
* Bitstamp::cancelOrder()
*
* @param int $id
*/
function cancelOrder($id=NULL){
if(
is_null($id))
throw new 
Exception('Order id is undefined');
return 
$this->bitstamp_query('cancel_order', array('id' => $id));
}

/**
* Bitstamp::balance()
*
*/
function balance(){
$balance $this->bitstamp_query('balance');
$this->balance $balance// Another variable to contain it.
return $balance;
}


/**
* Bitstamp::unconfirmedbtc()
*
*/
function unconfirmedbtc(){
$unconfirmedbtc $this->bitstamp_query('unconfirmed_btc');
$this->unconfirmedbtc $unconfirmedbtc// Another variable to contain it.
return $unconfirmedbtc;
}

/**
* Bitstamp::bitcoindepositaddress()
*
*/
function bitcoindepositaddress(){
$bitcoindepositaddress $this->bitstamp_query('bitcoin_deposit_address');
$this->bitcoindepositaddress $bitcoindepositaddress// Another variable to contain it.
return $bitcoindepositaddress;
}

/**
* Bitstamp::get_signature()
* Compute bitstamp signature
* @param float $nonce
*/
private function get_signature($nonce)
{

$message $nonce.$this->client_id.$this->key;

return 
strtoupper(hash_hmac('sha256'$message$this->secret));

}
}

Save it in the same directory as your other code as "bitstamp.php". Now change your other code to the following:
Code:
require_once('bitstamp.php');
$KEY = "YOUR KEY";
$SECRET = "YOUR SECRET";
$CLIENT_ID = "YOUR CLIENT ID";

$bs = new Bitstamp($KEY,$SECRET,$CLIENT_ID);

print_r($bs->ticker());

Then create an account on bitstamp, verify your account, then on their website, go to Security then API access. Copy and past the key, secret, and client id they provide you with into the matching variables in the code I just gave you. That should do the trick.

I submitted my papers to get verified and waiting...I'll let you know if everything work. Thanks for the help. I created another one and about to turn it into a wordpress widget. Anyone can get the code here: http://www.bitcoinvalues.net/bitcoin-forum?vasthtmlaction=viewtopic&t=11.0

DISCIPLINA — The First Blockchain For HR & Education
From core developers of Cardano, PoS minting, unique Web Of Trust & Privacy algorithms. Be the first, join us!
  WEBSITE  TELEGRAM  ANN  BOUNTY  LINKEDIN  WHITEPAPER  Referral Program 5%
colin012
Member
**
Offline Offline

Activity: 87
Merit: 10


View Profile
May 24, 2014, 04:55:25 PM
 #9

Can anyone help please

You are getting a bad result because you are not using the Bitstamp PHP api. Copy and paste the following code into a new php file:
Code:
<?php

/**
* @package Bitstamp API
* @author https://bxmediaus.com - BX MEDIA - PHP + Bitcoin. We are ready to work on your next bitcoin project. Only high quality coding. https://bxmediaus.com
* @version 0.1
* @access public
* @license http://www.opensource.org/licenses/LGPL-3.0
*/

class Bitstamp
{
private 
$key;
private 
$secret;
private 
$client_id;
public 
$redeemd// Redeemed code information
public $withdrew// Withdrawal information
public $info// Result from getInfo()
public $ticker// Current ticker (getTicker())
public $eurusd// Current eur/usd
/**
* Bitstamp::__construct()
* Sets required key and secret to allow the script to function
* @param Bitstamp API Key $key
* @param Bitstamp Secret $secret
* @return
*/
public function __construct($key$secret$client_id)
{
if (isset(
$secret) && isset($key) && isset($client_id))
{
$this->key $key;
$this->secret $secret;
$this->client_id $client_id;
} else
die(
"NO KEY/SECRET/CLIENT ID");
}
/**
* Bitstamp::bitstamp_query()
*
* @param API Path $path
* @param POST Data $req
* @return Array containing data returned from the API path
*/
public function bitstamp_query($path, array $req = array(), $verb 'post')
{
// API settings
$key $this->key;

// generate a nonce as microtime, with as-string handling to avoid problems with 32bits systems
$mt explode(' 'microtime());
$req['nonce'] = $mt[1] . substr($mt[0], 26);
$req['key'] = $key;
$req['signature'] = $this->get_signature($req['nonce']);


// generate the POST data string
        
$post_data http_build_query($req'''&');

// any extra headers
$headers = array();

// our curl handle (initialize if required)
static $ch null;
if (
is_null($ch))
{
$ch curl_init();
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
curl_setopt($chCURLOPT_USERAGENT,
'Mozilla/4.0 (compatible; MtGox PHP Client; ' php_uname('s') . '; PHP/' .
phpversion() . ')');
}
curl_setopt($chCURLOPT_URL'https://www.bitstamp.net/api/' $path .'/');
curl_setopt($chCURLOPT_SSL_VERIFYPEER1); // man-in-the-middle defense by verifying ssl cert.
curl_setopt($chCURLOPT_SSL_VERIFYHOST2); // man-in-the-middle defense by verifying ssl cert.
        
if ($verb == 'post')
        {
            
curl_setopt($chCURLOPT_POSTFIELDS$post_data);
        }
curl_setopt($chCURLOPT_HTTPHEADER$headers);

// run the query
$res curl_exec($ch);
if (
$res === false)
throw new \
Exception('Could not get reply: ' curl_error($ch));
$dec json_decode($restrue);
if (
is_null($dec))
throw new \
Exception('Invalid data received, please make sure connection is working and requested API exists');
return 
$dec;
}

/**
* Bitstamp::ticker()
* Returns current ticker from Bitstamp
* @return $ticker
*/
function ticker() {
$ticker $this->bitstamp_query('ticker', array(), 'get');
$this->ticker $ticker// Another variable to contain it.
return $ticker;
}

/**
* Bitstamp::eurusd()
* Returns current EUR/USD rate from Bitstamp
* @return $eurusd
*/
function eurusd() {
$eurusd $this->bitstamp_query('eur_usd', array(), 'get');
$this->eurusd $eurusd// Another variable to contain it.
return $eurusd;
}

/**
* Bitstamp::buyBTC()
*
* @param float $amount
* @param float $price
*/
function buyBTC($amount$price=NULL){
if(
is_null($price)){
if (!isset(
$this->ticker))
$this->ticker();

$price $this->ticker['ask'];
}

return 
$this->bitstamp_query('buy', array('amount' => $amount'price' => $price));
}

/**
* Bitstamp::sellBTC()
*
* @param float $amount
* @param float $price
*/
function sellBTC($amount$price=NULL){
if(
is_null($price)){
if (!isset(
$this->ticker))
$this->ticker();

$price $this->ticker['bid'];
}

return 
$this->bitstamp_query('sell', array('amount' => $amount'price' => $price));
}

/**
* Bitstamp::transactions()
*
* @param string $time
*/
function transactions($time='hour'){
return 
$this->bitstamp_query('transactions', array('time' => $time), 'get');
}

/**
* Bitstamp::orderBook()
*
* @param int $group
*/
function orderBook($group=1){
return 
$this->bitstamp_query('order_book', array('group' => $group), 'get');
}

/**
* Bitstamp::openOrders()
* List of open orders
*/
function openOrders(){
return 
$this->bitstamp_query('open_orders');
}

/**
* Bitstamp::cancelOrder()
*
* @param int $id
*/
function cancelOrder($id=NULL){
if(
is_null($id))
throw new 
Exception('Order id is undefined');
return 
$this->bitstamp_query('cancel_order', array('id' => $id));
}

/**
* Bitstamp::balance()
*
*/
function balance(){
$balance $this->bitstamp_query('balance');
$this->balance $balance// Another variable to contain it.
return $balance;
}


/**
* Bitstamp::unconfirmedbtc()
*
*/
function unconfirmedbtc(){
$unconfirmedbtc $this->bitstamp_query('unconfirmed_btc');
$this->unconfirmedbtc $unconfirmedbtc// Another variable to contain it.
return $unconfirmedbtc;
}

/**
* Bitstamp::bitcoindepositaddress()
*
*/
function bitcoindepositaddress(){
$bitcoindepositaddress $this->bitstamp_query('bitcoin_deposit_address');
$this->bitcoindepositaddress $bitcoindepositaddress// Another variable to contain it.
return $bitcoindepositaddress;
}

/**
* Bitstamp::get_signature()
* Compute bitstamp signature
* @param float $nonce
*/
private function get_signature($nonce)
{

$message $nonce.$this->client_id.$this->key;

return 
strtoupper(hash_hmac('sha256'$message$this->secret));

}
}

Save it in the same directory as your other code as "bitstamp.php". Now change your other code to the following:
Code:
require_once('bitstamp.php');
$KEY = "YOUR KEY";
$SECRET = "YOUR SECRET";
$CLIENT_ID = "YOUR CLIENT ID";

$bs = new Bitstamp($KEY,$SECRET,$CLIENT_ID);

print_r($bs->ticker());

Then create an account on bitstamp, verify your account, then on their website, go to Security then API access. Copy and past the key, secret, and client id they provide you with into the matching variables in the code I just gave you. That should do the trick.

I submitted my papers to get verified and waiting...I'll let you know if everything work. Thanks for the help. I created another one and about to turn it into a wordpress widget. Anyone can get the code here: http://www.bitcoinvalues.net/bitcoin-forum?vasthtmlaction=viewtopic&t=11.0

No problem. I hope everything turns out ok.

NXT Organization Marketing
BitcoinAverage
Full Member
***
Offline Offline

Activity: 147
Merit: 102



View Profile WWW
May 24, 2014, 05:03:25 PM
 #10

Jessy, rather than picking just one exchange you could use our API which provides a weighted average of around 40 exchanges currently:

https://api.bitcoinaverage.com/

example USD request:

https://api.bitcoinaverage.com/ticker/global/USD/

If you have want a PHP snippet please email us and we'll knock something up for you: bitcoinaverage@gmail.com

Free Live Price Data: https://BitcoinAverage.com/ - API: https://apiv2.bitcoinaverage.com/ - Twitter: https://twitter.com/BitcoinAverage
BitcoinAverage (Blockchain Data LTD) © 2012-2017
teamcryptonator
Hero Member
*****
Offline Offline

Activity: 749
Merit: 503

Blockchain Just Entered The Real World


View Profile
May 24, 2014, 07:25:17 PM
 #11

there's a ready-made solution, fully customizable widgets and price tickers https://www.cryptonator.com/widget

      ███████████████████████
     ███▄ ▄▄▄▄   ▄▄▄█▀▀  █████
    ███  █▀  ▀█▀▀▀       ▐█ ███
   ███  ▄██▄▄█▀▄▄▄        █▌ ███
  ███ ▄█▀  █     ▀█▄▄     ▐█  ▐██
 ███▄█▀    █        ▀█▄▄  ▄▄▄ ██
████▀      █           ▀██▀   ▀█ ██
 ██▀█▄     █          ▄█▀▀█▄▄▄█▀██
  ██ ▀█▄   █      ▄▄█▀▀    ▐█  ██
   ██  ▀█▄█▀▀█▄▄█▀▀        █▌ ██
    ███  █▄  ▄█▀█▄▄▄      █▌███
     ███  ▀▀▀▀     ▀▀▀█▄▄▐████
      ███████████████████████

 ▄▄       ▄▄▄        ▄▄   ▄▄▄▄▄ 
  ▀█▄   ▄█▀ ▀█▄    ▄█▀ ▄█▀▀   ▀▀█▄
    ▀█▄█▀     ▀█▄▄█▀  ▐█         █▌
    ▄█▀█▄      ▄█▀    ▐█         █▌
  ▄█▀   ▀█▄  ▄█▀       █▄       ▄█
▄█▀       ▀██▀          ▀▀█▄▄▄█▀▀


Network
BLOCKCHAIN JUST ENTERED THE REAL WORLD
..Decentralized Crypto-Location Oracle Network.
.........GET WHITELISTED FOR TOKEN SALE ( Limited )..........

.WHITE PAPER.  ││  ANN Thread  Telegram   Medium   Twitter   Reddit
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!