Hello everyone!
I am currently working on a UI for CEX.IO.
I am having a little trouble with the trading part tho.
Would anyone be willing to help me with that part?
All I need is a single PHP script that I can set type, amount, and price and send it over the API.
I tried and tried but I just don't understand what I am doing wrong.
Something like this would be perfect:
function cex_query($method, array $req = array()) {
// API settings
$key = ''; // your API-key
$secret = ''; // your Secret-key
$req['method'] = $method;
$mt = explode(' ', microtime());
$req['nonce'] = $mt[1];
// generate the POST data string
$post_data = http_build_query($req, '', '&');
$sign = hash_hmac('sha512', $post_data, $secret);
// generate the extra headers
$headers = array(
'Sign: '.$sign,
'Key: '.$key,
);
// 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; BTCE PHP client; '.php_uname('s').'; PHP/'.phpversion().')');
}
curl_setopt($ch, CURLOPT_URL, 'https://cex.io/api/place_order/GHS/BTC');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
// 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;
}
$result = cex_query('Trade', array('type' => 'buy', 'amount' => 1, 'price' => 10));
var_dump($result);
?>