CurrencyBitcoinLib.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. App::uses('HttpSocketLib', 'Tools.Lib');
  3. /**
  4. * Use Webservices to get current rates etc
  5. *
  6. * @author Mark Scherer
  7. * @license http://opensource.org/licenses/mit-license.php MIT
  8. */
  9. class CurrencyBitcoinLib {
  10. public $settings = [
  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 = []) {
  18. $options += $this->settings;
  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. *
  35. * @see http://bitcoincharts.com/about/markets-api/
  36. */
  37. public function bitcoincharts($options = []) {
  38. $options += $this->settings;
  39. $url = 'http://bitcoincharts.com/t/markets.json';
  40. $res = $this->_getBitcoincharts($url);
  41. if (!$res) {
  42. return false;
  43. }
  44. $array = [];
  45. foreach ($res as $val) {
  46. $array[$val['currency']] = $val;
  47. unset($array[$val['currency']]['currency']);
  48. }
  49. if (empty($options['currency'])) {
  50. return $array;
  51. }
  52. if (empty($array[$options['currency']])) {
  53. return false;
  54. }
  55. return $array[$options['currency']];
  56. }
  57. /**
  58. * @param array $options
  59. * - currency
  60. * - api
  61. */
  62. public function rate($options = []) {
  63. $options += $this->settings;
  64. $res = $this->{$options['api']}($options);
  65. if ($res && isset($res['sell'])) {
  66. // bitmarket
  67. $current = $res['sell'];
  68. } elseif ($res && isset($res['ask'])) {
  69. // bitcoincharts
  70. $current = $res['ask'];
  71. }
  72. if (isset($current)) {
  73. return $this->calcRate($current);
  74. }
  75. return false;
  76. }
  77. /**
  78. * Calc BTC relative to 1 baseCurrency
  79. *
  80. * @param float $value
  81. * @return float relativeValue
  82. */
  83. public function calcRate($current) {
  84. return 1.0 / (float)$current;
  85. }
  86. /**
  87. * Historic trade data
  88. *
  89. * @see http://bitcoincharts.com/about/markets-api/
  90. */
  91. public function trades() {
  92. //TODO
  93. }
  94. protected function _getBitmarket($url) {
  95. if (!($res = $this->_get($url))) {
  96. return false;
  97. }
  98. if (!($res = json_decode($res, true))) {
  99. return false;
  100. }
  101. return $res;
  102. }
  103. protected function _getBitcoincharts($url) {
  104. if (!($res = $this->_get($url))) {
  105. return false;
  106. }
  107. if (!($res = json_decode($res, true))) {
  108. return false;
  109. }
  110. return $res;
  111. }
  112. protected function _get($url) {
  113. $http = new HttpSocketLib();
  114. if (!($res = $http->fetch($url))) {
  115. return false;
  116. }
  117. return $res;
  118. }
  119. }