CurrencyBitcoinLib.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. App::uses('HttpSocketLib', 'Tools.Lib');
  3. /**
  4. * Use Webservices to get current rates etc
  5. *
  6. * @author Mark Scherer
  7. * @license MIT
  8. */
  9. class CurrencyBitcoinLib {
  10. public $settings = array(
  11. 'currency' => 'EUR', # set to NULL or empty for all
  12. 'api' => 'bitmarket', # bitmarket or bitcoincharts
  13. );
  14. /**
  15. * @see https://bitmarket.eu/api
  16. */
  17. public function bitmarket($options = array()) {
  18. $options = array_merge($this->settings, $options);
  19. $url = 'https://bitmarket.eu/api/ticker';
  20. $res = $this->_getBitmarket($url);
  21. if (!$res) {
  22. return false;
  23. }
  24. if (empty($options['currency'])) {
  25. return $res['currencies'];
  26. }
  27. if (empty($res['currencies'][$options['currency']])) {
  28. return false;
  29. }
  30. return $res['currencies'][$options['currency']];
  31. }
  32. /**
  33. * working
  34. * @see http://bitcoincharts.com/about/markets-api/
  35. */
  36. public function bitcoincharts($options = array()) {
  37. $options = array_merge($this->settings, $options);
  38. $url = 'http://bitcoincharts.com/t/markets.json';
  39. $res = $this->_getBitcoincharts($url);
  40. if (!$res) {
  41. return false;
  42. }
  43. $array = array();
  44. foreach ($res as $val) {
  45. $array[$val['currency']] = $val;
  46. unset($array[$val['currency']]['currency']);
  47. }
  48. if (empty($options['currency'])) {
  49. return $array;
  50. }
  51. if (empty($array[$options['currency']])) {
  52. return false;
  53. }
  54. return $array[$options['currency']];
  55. }
  56. /**
  57. * @param array $options
  58. * - currency
  59. * - api
  60. */
  61. public function rate($options = array()) {
  62. $options = array_merge($this->settings, $options);
  63. $res = $this->{$options['api']}($options);
  64. if ($res && isset($res['sell'])) {
  65. # bitmarket
  66. $current = $res['sell'];
  67. } elseif ($res && isset($res['ask'])) {
  68. # bitcoincharts
  69. $current = $res['ask'];
  70. }
  71. if (isset($current)) {
  72. return $this->calcRate($current);
  73. }
  74. return false;
  75. }
  76. /**
  77. * Calc BTC relative to 1 baseCurrency
  78. * @param float $value
  79. * @return float relativeValue
  80. */
  81. public function calcRate($current) {
  82. return 1.0 / (float)$current;
  83. }
  84. /**
  85. * Historic trade data
  86. * @see http://bitcoincharts.com/about/markets-api/
  87. */
  88. public function trades() {
  89. //TODO
  90. }
  91. protected function _getBitmarket($url) {
  92. if (!($res = $this->_get($url))) {
  93. return false;
  94. }
  95. if (!($res = json_decode($res, true))) {
  96. return false;
  97. }
  98. return $res;
  99. }
  100. protected function _getBitcoincharts($url) {
  101. if (!($res = $this->_get($url))) {
  102. return false;
  103. }
  104. if (!($res = json_decode($res, true))) {
  105. return false;
  106. }
  107. return $res;
  108. }
  109. protected function _get($url) {
  110. $http = new HttpSocketLib();
  111. if (!($res = $http->fetch($url))) {
  112. return false;
  113. }
  114. return $res;
  115. }
  116. }