1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
/**
* Created by PhpStorm.
* User: rlgyzhcn
* Date: 18-6-26
* Time: 下午7:22
*/
namespace common\service\exchange;
use common\helpers\Curl;
use Yii;
/**
* Class Exchange
* 交易所抽象类
* @package common\service\exchange
*/
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']
]);
}
}
/**
* 转化交易对为请求变量
*
* @param string $tag
* @param string $aim
* @return mixed
*/
abstract public function formatSymbol($tag = 'BTC', $aim = 'USD');
/**
* 获取支持查询行情的交易对
*
* @param string $tag
* @param string $aim
* @return bool
*/
public function symbolExists($tag = 'BTC', $aim = "USD")
{
$supported = $this->redis->smembers($this->supported_symbol);
if (is_array($supported) && in_array($this->formatSymbol($tag, $aim), $supported)) {
return true;
}
return false;
}
/**
* 获取交易对聚合行情信息
*
* @param string $tag
* @param string $aim
* @return mixed
*/
public function getTicker($tag = 'BTC', $aim = "USDT")
{
$symbol = $this->formatSymbol($tag, $aim);
$keys = $this->redis->hkeys($this->quotation_prefix . $symbol);
$values = $this->redis->hvals($this->quotation_prefix . $symbol);
return array_combine($keys, $values);
}
}