/includes/mtgox.php

https://github.com/MobiusL/simplecoin · PHP · 357 lines · 148 code · 48 blank · 161 comment · 3 complexity · 032293c0f239993b1fcb35a723839d00 MD5 · raw file

  1. <?php
  2. /**
  3. * Mt Gox Trade API Client
  4. *
  5. * History:
  6. *
  7. * 12-Apr-11
  8. * First version of the client
  9. *
  10. * @author Russell Smith <russell.smith@ukd1.co.uk>
  11. * @copyright UKD1 Limited 2011
  12. * @license licence.txt ISC license
  13. * @see https://www.mtgox.com/support/tradeAPI
  14. * @see https://github.com/ukd1/Mt-Gox-Trade-API-PHP-Client
  15. */
  16. /**
  17. * Order class for Mt Gox Orders
  18. *
  19. * @author Russell Smith <russell.smith@ukd1.co.uk>
  20. * @copyright UKD1 Limited 2011
  21. * @license licence.txt ISC license
  22. * @see https://www.mtgox.com/support/tradeAPI
  23. * @see https://github.com/ukd1/Mt-Gox-Trade-API-PHP-Client
  24. */
  25. class mtgox_order extends mtgox_base
  26. {
  27. private $data;
  28. /**
  29. *
  30. *
  31. * @param $user username for Mt Gox
  32. * @param $pass password for Mt Gox
  33. * @param null $data array of data about the order
  34. */
  35. public function __construct ($user, $pass, $data = null)
  36. {
  37. $this->user = $user;
  38. $this->pass = $pass;
  39. $this->data = $data;
  40. }
  41. public function __get ($key)
  42. {
  43. return isset($this->data[$key]) ? $this->data[$key] : null;
  44. }
  45. /**
  46. * Is this a sell order?
  47. * @return bool
  48. */
  49. public function is_sell_order ()
  50. {
  51. return $this->data['type'] === self::SELL_ORDER;
  52. }
  53. /**
  54. * Is this a buy order?
  55. * @return bool
  56. */
  57. public function is_buy_order ()
  58. {
  59. return $this->data['type'] === self::BUY_ORDER;
  60. }
  61. /**
  62. * Cancel the order over the API
  63. * @return bool true on success
  64. */
  65. public function cancel ()
  66. {
  67. return is_array($this->_post('cancelOrder.php', array('oid' => $this->data['oid'], 'type' => $this->data['type'])));
  68. }
  69. }
  70. /**
  71. * Mt Gox Trade API implementation
  72. *
  73. * @author Russell Smith <russell.smith@ukd1.co.uk>
  74. * @copyright UKD1 Limited 2011
  75. * @license licence.txt ISC license
  76. * @see https://www.mtgox.com/support/tradeAPI
  77. * @see https://github.com/ukd1/Mt-Gox-Trade-API-PHP-Client
  78. */
  79. class mtgox extends mtgox_base
  80. {
  81. /**
  82. * @param string $user username
  83. * @param string $pass password
  84. */
  85. public function __construct ($user, $pass)
  86. {
  87. $this->user = $user;
  88. $this->pass = $pass;
  89. }
  90. /**
  91. * Get your current balance
  92. *
  93. * @return array An array of your current USD / BTC balance
  94. */
  95. public function balance ()
  96. {
  97. return $this->_post('getFunds.php');
  98. }
  99. /**
  100. * Request a transfer of BTC
  101. *
  102. * @param string $address BTC address to transfer to
  103. * @param float $amount amount of BTC to transfer
  104. * @return array|false
  105. */
  106. public function widthdraw ($address, $amount)
  107. {
  108. return $this->_post('withdraw.php', array('bitcoin_address_to_send_to' => $address, 'amount' => $amount));
  109. }
  110. /**
  111. * Return an array of your current orders
  112. *
  113. * @param int $oid optionally just return a single order
  114. * @return array
  115. */
  116. public function orders ()
  117. {
  118. $response = $this->_post('getOrders.php', array('amount' => '#', 'price' => '#'));
  119. $orders = array();
  120. foreach ($response['orders'] as $_order)
  121. {
  122. $orders[] = new mtgox_order($this->user, $this->pass, $_order);
  123. }
  124. return $orders;
  125. }
  126. /**
  127. * Create a buy order for amount @ price
  128. *
  129. * @param int $amount amount of BTC to buy
  130. * @param float $price price to buy BTC at
  131. * @return array|bool
  132. */
  133. public function buy ($amount, $price)
  134. {
  135. return $this->_post('buyBTC.php', array('amount' => $amount, 'price' => $price));
  136. }
  137. /**
  138. * Create a sell order for amount @ price
  139. *
  140. * @param int $amount amount of BTC to sell
  141. * @param float $price price to sell BTC at
  142. * @return array|bool
  143. */
  144. public function sell ($amount, $price)
  145. {
  146. return $this->_post('sellBTC.php', array('amount' => $amount, 'price' => $price));
  147. }
  148. /**
  149. * Return an array of ticker data
  150. *
  151. * @return array
  152. */
  153. public function ticker ()
  154. {
  155. $ticker = $this->_get('data/ticker.php');
  156. return $ticker['ticker'];
  157. }
  158. /**
  159. * Return an array of market depth data
  160. * @return array
  161. */
  162. public function depth ()
  163. {
  164. return $this->_get('data/getDepth.php');
  165. }
  166. /**
  167. * Return an array of asks / bids
  168. * @return array
  169. */
  170. public function trades ()
  171. {
  172. return $this->_get('data/getTrades.php');
  173. }
  174. }
  175. /**
  176. * Abstract class which the main / order class extend
  177. *
  178. * @author Russell Smith <russell.smith@ukd1.co.uk>
  179. * @copyright UKD1 Limited 2011
  180. * @license licence.txt ISC license
  181. * @see https://www.mtgox.com/support/tradeAPI
  182. * @see https://github.com/ukd1/Mt-Gox-Trade-API-PHP-Client
  183. */
  184. abstract class mtgox_base
  185. {
  186. /**
  187. * @var username to use for authentication against the API
  188. */
  189. protected $user;
  190. /**
  191. * @var password to use for authentication against the API
  192. */
  193. protected $pass;
  194. /**
  195. * Current Mt Gox fee in percent (unused)
  196. */
  197. const MTGOX_FEE = 0.0065;
  198. /**
  199. * Mt Gox endpoint for the API
  200. */
  201. const ENDPOINT = 'https://www.mtgox.com/code/';
  202. /**
  203. * A timeout to control how long to wait for the API to respond in seconds
  204. */
  205. const TIMEOUT = 3;
  206. /**
  207. * User agent string which is sent which all requests
  208. */
  209. const USERAGENT = 'UKD1 MTGOX Client';
  210. /**
  211. * Sell Order type
  212. */
  213. const SELL_ORDER = 1;
  214. /**
  215. * Buy order type
  216. */
  217. const BUY_ORDER = 2;
  218. /**
  219. * Order status ACTIVE
  220. */
  221. const STATUS_ACTIVE = 1;
  222. /**
  223. * Order status INACTIVE (insufficent funds)
  224. */
  225. const STATUS_INSUFFICENT_FUNDS = 2;
  226. /**
  227. * Do a HTTP POST to the specified URI
  228. *
  229. * @param string $uri uri to post to (appended to the endpoint)
  230. * @param array $data array of post fields to pass
  231. * @return bool|array
  232. */
  233. protected function _post ($uri, $data = array())
  234. {
  235. $data['name'] = $this->user;
  236. $data['pass'] = $this->pass;
  237. $r = $this->_http('POST', $uri, $data);
  238. if ($r['http_code'] === 200)
  239. {
  240. return json_decode($r['result'], true);
  241. }
  242. else
  243. {
  244. return false;
  245. }
  246. }
  247. /**
  248. * Perform an HTTP GET
  249. *
  250. * @param $uri URI to get, appended to the endpoint url
  251. * @param array $data get parameters
  252. * @return bool|array
  253. */
  254. protected function _get ($uri, $data = array())
  255. {
  256. $r = $this->_http('GET', $uri, $data);
  257. if ($r['http_code'] === 200)
  258. {
  259. return json_decode($r['result'], true);
  260. }
  261. else
  262. {
  263. return false;
  264. }
  265. }
  266. /**
  267. * perform a HTTP request
  268. *
  269. * @param string $method HTTP method to use, currently supports GET|POST
  270. * @param string $uri URI to append to the endppint
  271. * @param array $data single dimensional key / value pairs of data to pass
  272. * @return array
  273. */
  274. protected function _http ($method, $uri, $data)
  275. {
  276. $url = self::ENDPOINT . $uri;
  277. $ch = curl_init();
  278. curl_setopt($ch, CURLOPT_USERAGENT, self::USERAGENT);
  279. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  280. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
  281. curl_setopt($ch, CURLOPT_TIMEOUT, self::TIMEOUT);
  282. switch ($method)
  283. {
  284. case 'POST':
  285. $post_fields = array();
  286. foreach ($data as $k=>$v) {
  287. array_push($post_fields, "$k=$v");
  288. }
  289. $post_fields = implode('&', $post_fields);
  290. curl_setopt($ch, CURLOPT_URL, $url);
  291. curl_setopt($ch, CURLOPT_POST, TRUE);
  292. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
  293. break;
  294. case 'GET':
  295. default:
  296. $get_fields = array();
  297. foreach ($data as $k=>$v) {
  298. array_push($get_fields, "$k=$v");
  299. }
  300. $url .= '?' . implode('&', $get_fields);
  301. curl_setopt($ch, CURLOPT_URL, $url);
  302. }
  303. $result = curl_exec ($ch);
  304. $tmp = curl_getinfo($ch);
  305. $tmp['result'] = $result;
  306. curl_close ($ch);
  307. return $tmp;
  308. }
  309. }