CEX.IO API - Logon & GetBalance example


SUBMITTED BY: Guest

DATE: Oct. 28, 2013, 5:13 p.m.

FORMAT: PHP

SIZE: 2.9 kB

HITS: 1299

  1. <?php
  2. /*
  3. * CEX.IO - Bitcoin
  4. *
  5. * Query Balance
  6. *
  7. * LIMITS: "Do not make more than 600 request per 10 minutes or we will ban your IP address." If you want more, add a list of proxy servers to CURL.
  8. *
  9. * Oct 27, 2013 - They just released their API access last week, so I thought a seed to get some bots started... -Shawn Reimerdes
  10. *
  11. * https://www.cex.io/api
  12. */
  13. // *** API settings ***
  14. $username = "enterhere"; // Username (NOTE: CASE SENSITIVE!!!)
  15. $key = 'enterhere'; // your API-key
  16. $secret = 'enterhere'; // your Secret-key
  17. $VERSION = '0.10 beta';
  18. // ___________________________________________________________________________________________ ____________ _ ___ _ __ _ .
  19. function cexio_query($path, array $req = array()) {
  20. $mt = explode(' ', microtime());
  21. $sign = strtoupper(hash_hmac("sha256", $mt[1] . $username . $key, $secret));
  22. $req['key'] = $key;
  23. $req['signature'] = $sign;
  24. $req['nonce'] = $mt[1];
  25. # generate the POST data string
  26. $post_data = http_build_query($req, '', '&');
  27. # generate the extra headers
  28. $headers = array(
  29. 'key: '.$key,
  30. 'signature: '.$sign,
  31. 'nonce:'.$mt[1]
  32. );
  33. // our curl handle (initialize if required)
  34. static $ch = null;
  35. if (is_null($ch)) {
  36. $ch = curl_init();
  37. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  38. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; CEX.IO PHP client v'.$VERSION.'; '.php_uname('s').'; PHP/'.phpversion().')');
  39. }
  40. curl_setopt($ch, CURLOPT_URL, $path);
  41. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  42. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  43. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  44. // run the query
  45. $res = curl_exec($ch);
  46. if ($res === false) throw new Exception('Could not get reply: '.curl_error($ch));
  47. $dec = json_decode($res, true);
  48. if (!$dec) throw new Exception('Invalid data received, please make sure connection is working and requested API exists');
  49. return $dec;
  50. }
  51. // ___________________________________________________________________________________________ ____________ _ ___ _ __ _ .
  52. // example 1: get balance
  53. var_dump(cexio_query('https://cex.io/api/balance/'));
  54. /* EXAMPLE OUTPUT:
  55. array(3) {
  56. ["timestamp"]=>
  57. string(10) "1382979452"
  58. ["BTC"]=>
  59. array(2) {
  60. ["available"]=>
  61. string(10) "1.41929714"
  62. ["orders"]=>
  63. string(10) "49.26658765"
  64. }
  65. ["GHS"]=>
  66. array(2) {
  67. ["available"]=>
  68. string(12) "1194.25621640"
  69. ["orders"]=>
  70. string(10) "30.30000000"
  71. }
  72. }
  73. */
  74. ?>

comments powered by Disqus