Commit 43ee5f02 authored by rlgy's avatar rlgy

bittrex api

parent 5a42a900
......@@ -69,7 +69,7 @@ class ExchangeBusiness
$f = false;//是否获取到行情
foreach (self::$exchanges as $exchange) {
/**
* @var $exchange \common\service\exchange\ExchangeInterface
* @var $exchange \common\service\exchange\Exchange
*/
$exchange = ExchangeFactory::createExchange($exchange);
if ($exchange->symbolExists($row['name'])) {
......@@ -85,7 +85,7 @@ class ExchangeBusiness
foreach (self::$exchanges as $exchange) {
/**
* @var $exchange \common\service\exchange\ExchangeInterface
* @var $exchange \common\service\exchange\Exchange
*/
$exchange = ExchangeFactory::createExchange($exchange);
if ($exchange->symbolExists($row['name'], 'btc')) {
......
......@@ -23,45 +23,24 @@ class Bitfinex extends Exchange implements ExchangeInterface
public function setSupportedSymbol()
{
/**
* @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);
$result = $this->ch->get('https://api.bitfinex.com/v1/symbols', false);
if ($result) {
foreach ($result as $item) {
$redis->sadd($this->supported_symbol, 't' . strtoupper($item));
$this->redis->sadd($this->supported_symbol, 't' . strtoupper($item));
}
}
}
public function setQuotation()
{
/**
* @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);
$symbols = $this->redis->smembers($this->supported_symbol);
$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) {
$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 @@
namespace common\service\exchange;
use common\helpers\Curl;
use Yii;
/**
......@@ -19,6 +20,27 @@ abstract class Exchange
{
protected $supported_symbol = '';
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
*/
public function symbolExists($tag = 'BTC', $aim = "USD")
{
/**
* @var $redis \yii\redis\Connection
*/
$redis = Yii::$app->redis;
$supported = $redis->smembers($this->supported_symbol);
$supported = $this->redis->smembers($this->supported_symbol);
if (is_array($supported) && in_array($this->formatSymbol($tag, $aim), $supported)) {
return true;
}
......@@ -59,12 +77,8 @@ abstract class Exchange
public function getTicker($tag = 'BTC', $aim = "USDT")
{
$symbol = $this->formatSymbol($tag, $aim);
/**
* @var $redis \yii\redis\Connection
*/
$redis = Yii::$app->redis;
$keys = $redis->hkeys($this->quotation_prefix . $symbol);
$values = $redis->hvals($this->quotation_prefix . $symbol);
$keys = $this->redis->hkeys($this->quotation_prefix . $symbol);
$values = $this->redis->hvals($this->quotation_prefix . $symbol);
return array_combine($keys, $values);
}
}
\ No newline at end of file
......@@ -20,24 +20,12 @@ class Hadax extends HuoBi
public function setSupportedSymbol()
{
$api = $this->base_url . '/v1/hadax/common/symbols';
$key = $this->supported_symbol;
$ch = new Curl();
//http代理
if (USER_PROXY) {
$ch->setOptions([
CURLOPT_PROXY => '127.0.0.1',
CURLOPT_PROXYPORT => 1080,
]);
}
$res = $ch->get($api, false);//json
$res = $this->ch->get($api, false);//json
if ($res && $res['status'] == 'ok') {
$data = $res['data'];
/**
* @var $redis \yii\redis\Connection
*/
$redis = Yii::$app->redis;
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
public function setSupportedSymbol()
{
$api = $this->base_url . '/v1/common/symbols';
$key = $this->supported_symbol;
$ch = new Curl();
//http代理
if (USER_PROXY) {
$ch->setOptions([
CURLOPT_PROXY => '127.0.0.1',
CURLOPT_PROXYPORT => 1080,
]);
}
$res = $ch->get($api, false);//json
$res = $this->ch->get($api, false);//json
if ($res && $res['status'] == 'ok') {
$data = $res['data'];
/**
* @var $redis \yii\redis\Connection
*/
$redis = Yii::$app->redis;
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
public function setQuotation()
{
$api = $this->base_url . '/market/tickers';
$ch = new Curl();
//http代理
if (USER_PROXY) {
$ch->setOptions([
CURLOPT_PROXY => '127.0.0.1',
CURLOPT_PROXYPORT => 1080,
]);
}
$res = $ch->get($api, false);
$res = $this->ch->get($api, false);
if ($res && $res['status'] == 'ok') {
$datas = $res['data'];
/**
* @var $redis \yii\redis\Connection
*/
$redis = Yii::$app->redis;
foreach ($datas as $item) {
$key = $this->quotation_prefix . $item['symbol'];
$redis->hmset($key, 'low', $item['low'], 'high', $item['high'], 'last', $item['close']);
$redis->sadd($this->supported_symbol, $item['symbol']);
$this->redis->hmset($key, 'low', $item['low'], 'high', $item['high'], 'last', $item['close']);
$this->redis->sadd($this->supported_symbol, $item['symbol']);
}
}
}
......
......@@ -37,7 +37,7 @@ class ExchangeController extends Controller
public function actionTest()
{
$a = new \common\service\exchange\Bitfinex();
$a = new \common\service\exchange\Bittrex();
// $a->setSupportedSymbol();
$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