Matt Matt - 2 months ago 79
PHP Question

PHP implementation of Bitstamp API

I am trying to implement a PHP API to bitstamp to do a re-occurring transaction by placing the PHP code in the crontab.

I am trying to cause the API to communicate with bitstamp to buy X amount of BTC per execution (and then control the frequency from the crontab), this should be the very definition of the basic implementation.

Here is the joy, I am absolutely not a PHP coder. The guys at BX Media were nice enough to post their PHP frame on github: (

However, the way I understand what they have done is that I must create the "parent" PHP to then include their API code which would also include my credentials

So I put together the most basic PHP code


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

// print_r($bs->ticker());

$bs->buyBTC(0.01); // buy 0.01 bitcoins at ask price
//$bs->bitstamp_query("buy", array('amount'=>'0.05','price'=>'50'));


note: that "bitstamp.php" is in the same directory and is what is presently on Github.


* @package Bitstamp API
* @author - BX MEDIA - PHP + Bitcoin. We are ready to work on your next bitcoin project. Only high quality coding.
* @version 0.1
* @access public
* @license

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
* 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())
// 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], 2, 6);
$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($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT,
'Mozilla/4.0 (compatible; MtGox PHP client; ' . php_uname('s') . '; PHP/' .
phpversion() . ')');
curl_setopt($ch, CURLOPT_URL, '' . $path .'/');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_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($res, true);
if (!$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');
$this->ticker = $ticker; // Another variable to contain it.
return $ticker;

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

* Bitstamp::buyBTC()
* @param float $amount
function buyBTC($amount){

if (!isset($ticker))

$ticker = $this->ticker;

return $this->bitstamp_query('buy', array('amount' => $amount, 'price' => $ticker['ask']));


* Bitstamp::sellBTC()
* @param float $amount
* @param float $price
* @param string $currency
function sellBTC($amount){

if (!isset($ticker))

$ticker = $this->ticker;

return $this->bitstamp_query('sell', array('amount' => $amount, 'price' => $ticker['bid']));


* 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));


I am getting a failure on the execution. As the author of the Bitstamp API has it apparently working with his clients, I assume the error is on my "parent" PHP code. (Note: I am using the real key and secret on my local version).

Anyone have any experience with this API or in general or suggestions?

Answer Source

As the author of this api (I am CEO of Bx Media):

You need to remake the constructor like this:

$bs = new Bitstamp("put your key here","put your secret here","put your client ID here");

Ie. you actually put your key/secret/ID between the speech marks.

In PHP, when you put a $ sign, it sets something as a variable.

See more information here:

If you want to use variables, you can also use IMSoP's solution.

Please note, we also updated the library tonight with some new functions. So please update your repo to the latest commit to get the new code.