Skip to content

Instantly share code, notes, and snippets.

@JamieWritesCode
Last active May 11, 2017 02:42
Show Gist options
  • Save JamieWritesCode/c5ff9f68353438f0a7dfff22ff42bcf5 to your computer and use it in GitHub Desktop.
Save JamieWritesCode/c5ff9f68353438f0a7dfff22ff42bcf5 to your computer and use it in GitHub Desktop.
<?php
namespace App\Helpers;
/*
| BTCMarkets
| Exchange is in AUD.
|
| https://github.com/BTCMarkets/API/wiki/Introduction
|
*/
class BTCMarkets {
private $key;
private $secret;
private $url;
function __construct()
{
$this->key = env('BTCM_KEY');
$this->secret = env('BTCM_SECRET');
$this->url = 'https://api.btcmarkets.net';
}
public function history($count = 10, $since)
{
$response = $this->request('/order/history', [
'currency' => 'AUD',
'instrument' => 'BTC',
'limit' => $count,
'since' => $since,
]);
if ($response['success']) {
return $response;
}
}
private function request($method, $data = [])
{
$data = json_encode($data);
$nonce = floor(microtime(true) * 1000); // API requires timestamp in milliseconds
// build query string
$tosign = implode('\n', [$method, $nonce, $data]);
// sign query string
$signed = base64_encode(hash_hmac('sha512', $tosign, base64_decode($this->secret, true)));
// generate the extra headers
$headers = [
"Accept: application/json",
"Accept-Charset: UTF-8",
"Content-Type: application/json",
"apikey: " . $this->key,
"signature: " . $signed,
"timestamp: " . $nonce,
];
// 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; BTC Markets Client; '.php_uname('s').'; PHP/'.phpversion().')');
}
curl_setopt($ch, CURLOPT_URL, $this->url . $method);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$response = curl_exec($ch);
if ($response === false) {
return false;
}
$result = json_decode($response, true);
if (!$result) {
return false;
}
return $result;
}
}
@JamieWritesCode
Copy link
Author

Removed Log class for clarity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment