Commit 4e52c5a3 authored by shajiaiming's avatar shajiaiming

okex ticker

parent 784121e5
......@@ -17,16 +17,17 @@ class TickerController extends BaseController
if (!in_array($exchange, $exchange_arr)) {
$msg = '不存在的交易平台';
$code = -1;
$data = [];
goto doEnd;
}
$builder = ExchangeBuilderFactory::create($exchange);
$result = $builder->getTicker();
$code = $result['code'];
$data = $result['ticker'];
$msg = 'success';
doEnd :
return ['code' => $code, 'msg' => $msg];
return ['code' => $code, 'msg' => $msg, 'data' => $data];
}
public function actionGameTradeUpdate()
......
......@@ -15,17 +15,20 @@ class BinanceBuilder
protected $base_url = 'https://api.binance.com';
protected $base_coin = ['ETH', 'BTC', 'USDT', 'BTC'];
function __construct()
public function __construct()
{
}
function getTicker()
public function getTicker()
{
$curl = new Curl();
$api = $this->base_url . '/api/v1/ticker/24hr';
$res = $curl->get($api, false);
$code = -1;
$ticker = [];
if (is_array($res)) {
$code = 0;
foreach ($res as $val) {
foreach ($this->base_coin as $k => $coin) {
$explode_arr = explode($coin, $val['symbol']);
......@@ -40,6 +43,6 @@ class BinanceBuilder
}
}
}
echo json_encode($ticker);exit;
return ['code' => $code, 'ticker' => $ticker];
}
}
\ No newline at end of file
<?php
namespace common\service\exchange\factory;
/**
* Created by PhpStorm.
* User: jiaming
......@@ -8,7 +6,43 @@ namespace common\service\exchange\factory;
* Time: 10:10
*/
namespace common\service\exchange\factory;
use linslin\yii2\curl\Curl;
class HuobiBuilder
{
protected $base_url = 'https://api.huobi.pro';
protected $base_coin = ['ETH', 'BTC', 'USDT', 'BTC'];
public function __construct()
{
}
public function getTicker()
{
$curl = new Curl();
$api = $this->base_url . '/market/tickers';
$res = $curl->get($api, false);
$code = -1;
$ticker = [];
if (isset($res['status']) && 'ok' == $res['status']) {
$code = 0;
foreach ($res['data'] as $val) {
foreach ($this->base_coin as $k => $coin) {
$explode_arr = explode($coin, $val['symbol']);
if (2 == count($explode_arr) && empty($explode_arr[1])) {
$ticker[$val['symbol']]['symbol'] = $explode_arr[0] . '/' . $coin;
$ticker[$val['symbol']]['close'] = (float)sprintf("%0.4f", $val['close']);
$ticker[$val['symbol']]['change'] = (float)sprintf(($val['close'] - $val['open']) / $val['open'] * 100);
$ticker[$val['symbol']]['high'] = (float)sprintf("%0.4f", $val['highPrice']);
$ticker[$val['symbol']]['low'] = (float)sprintf("%0.4f", $val['lowPrice']);
$ticker[$val['symbol']]['vol'] = (float)sprintf("%0.4f", $val['vol']);
}
}
}
}
return ['code' => $code, 'ticker' => $ticker];
}
}
\ No newline at end of file
<?php
namespace common\service\exchange\factory;
/**
* Created by PhpStorm.
* User: jiaming
* Date: 2019/8/15
* Time: 10:10
*/
namespace common\service\exchange\factory;
use Yii;
use linslin\yii2\curl\Curl;
class OkexBuilder
{
protected $base_url = 'https://api.huobi.pro';
protected $base_coin = ['ETH', 'BTC', 'USDT', 'BTC'];
protected $redis;
protected $supported_symbol = 'supported_symbol_bitfinex_v2';
public function __construct()
{
$this->redis = Yii::$app->redis;
}
public function getSymbol()
{
$curl = new Curl();
$api = $this->base_url . '/v2/spot/markets/products';
$res = $curl->get($api, false);
$res = json_decode($res, true);
if (isset($res['code']) && 0 == $res['code']) {
foreach ($res['data'] as $val) {
if ($this->redis->sismember($this->supported_symbol, $val['symbol'])) continue;
$this->redis->sadd($this->supported_symbol, $val['symbol']);
}
}
}
public function getTicker()
{
$symbols = $this->redis->smembers($this->supported_symbol);
if (empty($symbols)) $this->getSymbol();
$symbols = $this->redis->smembers($this->supported_symbol);
$curl = new Curl();
$code = -1;
$ticker = [];
foreach ($symbols as $symbol) {
$api = $this->base_url . '/market/tickers?symbol=' . $symbol;
$res = $curl->get($api, false);
$res = json_decode($res, true);
if (isset($res['ticker'])) {
$code = 0;
$symbol = strtoupper(str_replace('_', '/', $symbol));
$ticker[$symbol]['symbol'] = $symbol;
$ticker[$symbol]['close'] = $res['ticker']['last'];
//$ticker[$symbol]['change'] = (float)sprintf(($val['close'] - $val['open']) / $val['open'] * 100);
$ticker[$symbol]['high'] = $res['ticker']['high'];
$ticker[$symbol]['low'] = $res['ticker']['low'];
$ticker[$symbol]['vol'] = $res['ticker']['last'];
}
}
return ['code' => $code, 'ticker' => $ticker];
}
}
\ No newline at end of file
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