Commit 43ee5f02 authored by rlgy's avatar rlgy

bittrex api

parent 5a42a900
...@@ -69,7 +69,7 @@ class ExchangeBusiness ...@@ -69,7 +69,7 @@ class ExchangeBusiness
$f = false;//是否获取到行情 $f = false;//是否获取到行情
foreach (self::$exchanges as $exchange) { foreach (self::$exchanges as $exchange) {
/** /**
* @var $exchange \common\service\exchange\ExchangeInterface * @var $exchange \common\service\exchange\Exchange
*/ */
$exchange = ExchangeFactory::createExchange($exchange); $exchange = ExchangeFactory::createExchange($exchange);
if ($exchange->symbolExists($row['name'])) { if ($exchange->symbolExists($row['name'])) {
...@@ -85,7 +85,7 @@ class ExchangeBusiness ...@@ -85,7 +85,7 @@ class ExchangeBusiness
foreach (self::$exchanges as $exchange) { foreach (self::$exchanges as $exchange) {
/** /**
* @var $exchange \common\service\exchange\ExchangeInterface * @var $exchange \common\service\exchange\Exchange
*/ */
$exchange = ExchangeFactory::createExchange($exchange); $exchange = ExchangeFactory::createExchange($exchange);
if ($exchange->symbolExists($row['name'], 'btc')) { if ($exchange->symbolExists($row['name'], 'btc')) {
......
...@@ -23,45 +23,24 @@ class Bitfinex extends Exchange implements ExchangeInterface ...@@ -23,45 +23,24 @@ class Bitfinex extends Exchange implements ExchangeInterface
public function setSupportedSymbol() public function setSupportedSymbol()
{ {
/** $result = $this->ch->get('https://api.bitfinex.com/v1/symbols', false);
* @var $redis \yii\redis\Connection
*/
$redis = Yii::$app->redis;
$ch = new Curl();
//http代理
if (Yii::$app->params['http_proxy']['use']) {
$ch->setOptions([
CURLOPT_PROXY => Yii::$app->params['http_proxy']['host'],
CURLOPT_PROXYPORT => Yii::$app->params['http_proxy']['port'],
]);
}
$result = $ch->get('https://api.bitfinex.com/v1/symbols', false);
if ($result) { if ($result) {
foreach ($result as $item) { foreach ($result as $item) {
$redis->sadd($this->supported_symbol, 't' . strtoupper($item)); $this->redis->sadd($this->supported_symbol, 't' . strtoupper($item));
} }
} }
} }
public function setQuotation() public function setQuotation()
{ {
/** $symbols = $this->redis->smembers($this->supported_symbol);
* @var $redis \yii\redis\Connection
*/
$redis = Yii::$app->redis;
$ch = new Curl();
//http代理
if (Yii::$app->params['http_proxy']['use']) {
$ch->setOptions([
CURLOPT_PROXY => Yii::$app->params['http_proxy']['host'],
CURLOPT_PROXYPORT => Yii::$app->params['http_proxy']['port'],
]);
}
$symbols = $redis->smembers($this->supported_symbol);
$query = '?symbols=' . implode(',', $symbols); $query = '?symbols=' . implode(',', $symbols);
$res = $ch->get('https://api.bitfinex.com/v2/tickers' . $query, false); $res = $this->ch->get('https://api.bitfinex.com/v2/tickers' . $query, false);
foreach ($res as $item) { foreach ($res as $item) {
$redis->hmset($this->quotation_prefix . $item[0], 'low', $item[10], 'high', $item['9'], 'last', $item[7]); $this->redis->hmset($this->quotation_prefix . $item[0],
'low', $item[10],
'high', $item['9'],
'last', $item[7]);
} }
} }
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
namespace common\service\exchange; namespace common\service\exchange;
use common\helpers\Curl;
use Yii; use Yii;
/** /**
...@@ -19,6 +20,27 @@ abstract class Exchange ...@@ -19,6 +20,27 @@ abstract class Exchange
{ {
protected $supported_symbol = ''; protected $supported_symbol = '';
protected $quotation_prefix = ''; protected $quotation_prefix = '';
/**
* @var $redis \yii\redis\Connection
*/
protected $redis;
/**
* @var $ch \common\helpers\Curl
*/
protected $ch;
public function __construct()
{
$this->redis = Yii::$app->redis;
$this->ch = new Curl();
if (Yii::$app->params['http_proxy']['use']) {
$this->ch->setOptions([
CURLOPT_PROXY => Yii::$app->params['http_proxy']['host'],
CURLOPT_PROXYPORT => Yii::$app->params['http_proxy']['port']
]);
}
}
/** /**
* 转化交易对为请求变量 * 转化交易对为请求变量
...@@ -38,11 +60,7 @@ abstract class Exchange ...@@ -38,11 +60,7 @@ abstract class Exchange
*/ */
public function symbolExists($tag = 'BTC', $aim = "USD") public function symbolExists($tag = 'BTC', $aim = "USD")
{ {
/** $supported = $this->redis->smembers($this->supported_symbol);
* @var $redis \yii\redis\Connection
*/
$redis = Yii::$app->redis;
$supported = $redis->smembers($this->supported_symbol);
if (is_array($supported) && in_array($this->formatSymbol($tag, $aim), $supported)) { if (is_array($supported) && in_array($this->formatSymbol($tag, $aim), $supported)) {
return true; return true;
} }
...@@ -59,12 +77,8 @@ abstract class Exchange ...@@ -59,12 +77,8 @@ abstract class Exchange
public function getTicker($tag = 'BTC', $aim = "USDT") public function getTicker($tag = 'BTC', $aim = "USDT")
{ {
$symbol = $this->formatSymbol($tag, $aim); $symbol = $this->formatSymbol($tag, $aim);
/** $keys = $this->redis->hkeys($this->quotation_prefix . $symbol);
* @var $redis \yii\redis\Connection $values = $this->redis->hvals($this->quotation_prefix . $symbol);
*/
$redis = Yii::$app->redis;
$keys = $redis->hkeys($this->quotation_prefix . $symbol);
$values = $redis->hvals($this->quotation_prefix . $symbol);
return array_combine($keys, $values); return array_combine($keys, $values);
} }
} }
\ No newline at end of file
...@@ -20,24 +20,12 @@ class Hadax extends HuoBi ...@@ -20,24 +20,12 @@ class Hadax extends HuoBi
public function setSupportedSymbol() public function setSupportedSymbol()
{ {
$api = $this->base_url . '/v1/hadax/common/symbols'; $api = $this->base_url . '/v1/hadax/common/symbols';
$key = $this->supported_symbol; $res = $this->ch->get($api, false);//json
$ch = new Curl();
//http代理
if (USER_PROXY) {
$ch->setOptions([
CURLOPT_PROXY => '127.0.0.1',
CURLOPT_PROXYPORT => 1080,
]);
}
$res = $ch->get($api, false);//json
if ($res && $res['status'] == 'ok') { if ($res && $res['status'] == 'ok') {
$data = $res['data']; $data = $res['data'];
/**
* @var $redis \yii\redis\Connection
*/
$redis = Yii::$app->redis;
foreach ($data as $item) { foreach ($data as $item) {
$redis->sadd($key, $this->formatSymbol($item['base-currency'], $item['quote-currency'])); $this->redis->sadd($this->supported_symbol,
$this->formatSymbol($item['base-currency'], $item['quote-currency']));
} }
} }
} }
......
...@@ -25,24 +25,12 @@ class HuoBi extends Exchange implements ExchangeInterface ...@@ -25,24 +25,12 @@ class HuoBi extends Exchange implements ExchangeInterface
public function setSupportedSymbol() public function setSupportedSymbol()
{ {
$api = $this->base_url . '/v1/common/symbols'; $api = $this->base_url . '/v1/common/symbols';
$key = $this->supported_symbol; $res = $this->ch->get($api, false);//json
$ch = new Curl();
//http代理
if (USER_PROXY) {
$ch->setOptions([
CURLOPT_PROXY => '127.0.0.1',
CURLOPT_PROXYPORT => 1080,
]);
}
$res = $ch->get($api, false);//json
if ($res && $res['status'] == 'ok') { if ($res && $res['status'] == 'ok') {
$data = $res['data']; $data = $res['data'];
/**
* @var $redis \yii\redis\Connection
*/
$redis = Yii::$app->redis;
foreach ($data as $item) { foreach ($data as $item) {
$redis->sadd($key, $this->formatSymbol($item['base-currency'], $item['quote-currency'])); $this->redis->sadd($this->supported_symbol,
$this->formatSymbol($item['base-currency'], $item['quote-currency']));
} }
} }
} }
...@@ -50,25 +38,13 @@ class HuoBi extends Exchange implements ExchangeInterface ...@@ -50,25 +38,13 @@ class HuoBi extends Exchange implements ExchangeInterface
public function setQuotation() public function setQuotation()
{ {
$api = $this->base_url . '/market/tickers'; $api = $this->base_url . '/market/tickers';
$ch = new Curl(); $res = $this->ch->get($api, false);
//http代理
if (USER_PROXY) {
$ch->setOptions([
CURLOPT_PROXY => '127.0.0.1',
CURLOPT_PROXYPORT => 1080,
]);
}
$res = $ch->get($api, false);
if ($res && $res['status'] == 'ok') { if ($res && $res['status'] == 'ok') {
$datas = $res['data']; $datas = $res['data'];
/**
* @var $redis \yii\redis\Connection
*/
$redis = Yii::$app->redis;
foreach ($datas as $item) { foreach ($datas as $item) {
$key = $this->quotation_prefix . $item['symbol']; $key = $this->quotation_prefix . $item['symbol'];
$redis->hmset($key, 'low', $item['low'], 'high', $item['high'], 'last', $item['close']); $this->redis->hmset($key, 'low', $item['low'], 'high', $item['high'], 'last', $item['close']);
$redis->sadd($this->supported_symbol, $item['symbol']); $this->redis->sadd($this->supported_symbol, $item['symbol']);
} }
} }
} }
......
...@@ -37,7 +37,7 @@ class ExchangeController extends Controller ...@@ -37,7 +37,7 @@ class ExchangeController extends Controller
public function actionTest() public function actionTest()
{ {
$a = new \common\service\exchange\Bitfinex(); $a = new \common\service\exchange\Bittrex();
// $a->setSupportedSymbol(); // $a->setSupportedSymbol();
$a->setQuotation(); $a->setQuotation();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment