CurrencyBitcoinLib.php 2.7 KB

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