浏览代码

BitcoinLib

euromark 12 年之前
父节点
当前提交
03e6ccf8f5
共有 2 个文件被更改,包括 187 次插入0 次删除
  1. 137 0
      Lib/CurrencyBitcoinLib.php
  2. 50 0
      Test/Case/Lib/CurrencyBitcoinLibTest.php

+ 137 - 0
Lib/CurrencyBitcoinLib.php

@@ -0,0 +1,137 @@
+<?php
+App::uses('HttpSocketLib', 'Tools.Lib');
+
+/**
+ * Use Webservices to get current rates etc
+ *
+ * @author Mark Scherer
+ * @license MIT
+ * 2010-09-19 ms
+ */
+class CurrencyBitcoinLib {
+
+	public $settings = array(
+		'currency' => 'EUR', # set to NULL or empty for all
+		'api' => 'bitmarket', # bitmarket or bitcoincharts
+	);
+
+	/**
+	 * @see https://bitmarket.eu/api
+	 * 2011-10-06 ms
+	 */
+	public function bitmarket($options = array()) {
+		$options = array_merge($this->settings, $options);
+		$url = 'https://bitmarket.eu/api/ticker';
+		$res = $this->_getBitmarket($url);
+
+		if (!$res) {
+			return false;
+		}
+		if (empty($options['currency'])) {
+			return $res['currencies'];
+		}
+		if (empty($res['currencies'][$options['currency']])) {
+			return false;
+		}
+		return $res['currencies'][$options['currency']];
+	}
+
+	/**
+	 * working
+	 * @see http://bitcoincharts.com/about/markets-api/
+	 * 2011-10-06 ms
+	 */
+	public function bitcoincharts($options = array()) {
+		$options = array_merge($this->settings, $options);
+		$url = 'http://bitcoincharts.com/t/markets.json';
+		$res = $this->_getBitcoincharts($url);
+		if (!$res) {
+			return false;
+		}
+		$array = array();
+		foreach ($res as $val) {
+			$array[$val['currency']] = $val;
+			unset($array[$val['currency']]['currency']);
+		}
+
+		if (empty($options['currency'])) {
+			return $array;
+		}
+		if (empty($array[$options['currency']])) {
+			return false;
+		}
+		return $array[$options['currency']];
+	}
+
+	/**
+	 * @param array $options
+	 * - currency
+	 * - api
+	 * 2011-10-07 ms
+	 */
+	public function rate($options = array()) {
+		$options = array_merge($this->settings, $options);
+		$res = $this->{$options['api']}($options);
+
+		if ($res && isset($res['sell'])) {
+			# bitmarket
+			$current = $res['sell'];
+		} elseif ($res && isset($res['ask'])) {
+			# bitcoincharts
+			$current = $res['ask'];
+		}
+		if (isset($current)) {
+			return $this->calcRate($current);
+		}
+		return false;
+	}
+
+	/**
+	 * calc BTC relative to 1 baseCurrency
+	 * @param float $value
+	 * @return float $relativeValue
+	 * 2011-10-07 ms
+	 */
+	public function calcRate($current) {
+		return 1.0 / (float)$current;
+	}
+
+	/**
+	 * historic trade data
+	 * @see http://bitcoincharts.com/about/markets-api/
+	 * 2011-10-06 ms
+	 */
+	public function trades() {
+		//TODO
+	}
+
+	protected function _getBitmarket($url) {
+		if (!($res = $this->_get($url))) {
+			return false;
+		}
+		if (!($res = json_decode($res, true))) {
+			return false;
+		}
+		return $res;
+	}
+
+	protected function _getBitcoincharts($url) {
+		if (!($res = $this->_get($url))) {
+			return false;
+		}
+		if (!($res = json_decode($res, true))) {
+			return false;
+		}
+		return $res;
+	}
+
+
+	protected function _get($url) {
+		$http = new HttpSocketLib();
+		if (!($res = $http->fetch($url))) {
+			return false;
+		}
+		return $res;
+	}
+
+}

+ 50 - 0
Test/Case/Lib/CurrencyBitcoinLibTest.php

@@ -0,0 +1,50 @@
+<?php
+
+App::uses('CurrencyBitcoinLib', 'Tools.Lib');
+App::uses('MyCakeTestCase', 'Tools.TestSuite');
+
+class CurrencyBitcoinLibTest extends MyCakeTestCase {
+
+	public function setUp() {
+		parent::setUp();
+
+		$this->CurrencyBitcoin = new CurrencyBitcoinLib();
+	}
+
+	/**
+	 * 2011-10-07 ms
+	 */
+	public function testBitmarket() {
+		echo $this->_header('bitmarket - '.$this->CurrencyBitcoin->settings['currency']);
+		$is = $this->CurrencyBitcoin->bitmarket();
+		$this->debug($is);
+		//$this->assertFalse($is);
+	}
+
+	/**
+	 * 2011-10-07 ms
+	 */
+	public function testBitcoincharts() {
+		echo $this->_header('bitcoincharts - '.$this->CurrencyBitcoin->settings['currency']);
+		$is = $this->CurrencyBitcoin->bitcoincharts();
+		$this->debug($is);
+		//$this->assertFalse($is);
+	}
+
+	/**
+	 * 2011-10-07 ms
+	 */
+	public function testRate() {
+		$this->skipIf(true, 'TODO!');
+
+		echo $this->_header('rate - bitmarket - '.$this->CurrencyBitcoin->settings['currency']);
+		$is = $this->CurrencyBitcoin->rate();
+		$this->debug($is);
+		$this->assertTrue(is_numeric($is) && $is > 0 && $is < 100);
+
+		echo $this->_header('rate - bitcoincharts - '.$this->CurrencyBitcoin->settings['currency']);
+		$is = $this->CurrencyBitcoin->rate(array('api'=>'bitcoincharts'));
+		$this->debug($is);
+		$this->assertTrue(is_numeric($is) && $is > 0 && $is < 100);
+	}
+}