Last active
January 3, 2016 01:39
-
-
Save svasva/8390710 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function api_query($http_method, $method, array $req = array()) { | |
$key = ''; | |
$secret = ''; | |
$post_data = $http_method == 'POST' ? json_encode($req) : ''; | |
$sign = hash_hmac('sha512', $post_data, $secret); | |
$headers = array( | |
'Content-type: application/json', | |
'API-Key: '.$key, | |
'API-Sign: '.$sign | |
); | |
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; CoinEx API PHP client; '.php_uname('s').'; PHP/'.phpversion().')'); | |
} | |
curl_setopt($ch, CURLOPT_URL, 'https://coinex.pw/api/v2/'.$method); | |
if ($http_method == 'POST') curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); | |
$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; | |
} | |
$req = array(); | |
$order = array(); | |
$order['trade_pair_id'] = 15; | |
$order['amount'] = 100000000; | |
$order['rate'] = 500000; | |
$order['bid'] = false; | |
$req['order'] = $order; | |
$result = api_query('POST', 'orders', $req); | |
#$result = api_query('POST', 'orders/44685/cancel'); | |
#$result = api_query('GET', 'balances'); | |
echo print_r($result), "\n"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Was this code not meant to be run? I'm receiving error 422 Unprocessable Entity using my API keys. (I've been having difficulties testing my own script and decided to test this for insights.)