base_url . '/api/v1/ticker/24hr'; $res = $curl->get($api, false); $ticker = []; if (is_array($res)) { $this->code = 0; foreach ($res as $val) { foreach ($this->basic_coin as $k => $coin) { $explode_arr = explode($coin, $val['symbol']); if (2 == count($explode_arr) && empty($explode_arr[1])) { $temp = []; $temp['symbol'] = $explode_arr[0] . '/' . $coin; $temp['currency'] = strtoupper($explode_arr[0]); $temp['base_currency'] = strtoupper($coin); $temp['close'] = number_format($val['lastPrice'], 6, '.', ''); $temp['close_usd'] = (float)sprintf("%0.6f", $val['lastPrice'] * $this->basic_price[$coin]['usd']); $temp['close_rmb'] = (float)sprintf("%0.4f", $val['lastPrice'] * $this->basic_price[$coin]['rmb']); $temp['change'] = (float)sprintf("%0.4f", $val['priceChangePercent']); $temp['high_usd'] = (float)sprintf("%0.4f", $val['highPrice'] * $this->basic_price[$coin]['usd']); $temp['low_usd'] = (float)sprintf("%0.4f", $val['lowPrice'] * $this->basic_price[$coin]['usd']); $temp['high_rmb'] = (float)sprintf("%0.4f", $val['highPrice'] * $this->basic_price[$coin]['rmb']); $temp['low_rmb'] = (float)sprintf("%0.4f", $val['lowPrice'] * $this->basic_price[$coin]['rmb']); $temp['vol'] = (float)sprintf("%0.4f", $val['volume']); $temp['optional'] = false; $temp['platform_zh'] = '币安'; $temp['platform_us'] = 'binance'; array_push($ticker, $temp); break; } } } } return ['code' => $this->code, 'ticker' => $ticker]; } public function getTickerFromCache($page = 1) { $size = 0; for ($i = 0; $i < $page; $i++) { $size += 50; $this->end = $size; } $this->start = $this->end - 50; $this->end = $this->end - 1; $keys = $this->redis->smembers($this->supported_symbol); if (false == $this->redis->exists($this->supported_symbol_list)) { foreach ($keys as $val) { foreach ($this->basic_coin as $k => $coin) { $explode_arr = explode($coin, $val); if (2 == count($explode_arr) && empty($explode_arr[1])) { $this->redis->lpush($this->supported_symbol_list, $val); } } } } $keys = $this->redis->lrange($this->supported_symbol_list, $this->start, $this->end); $ticker = []; foreach ($keys as $val) { foreach ($this->basic_coin as $k => $coin) { $explode_arr = explode($coin, $val); if (2 == count($explode_arr) && empty($explode_arr[1])) { list($low, $high, $close, $open, $vol) = $this->redis->hmget($this->quotation_prefix . strtolower($val), 'low', 'high', 'last', 'open', 'vol'); $temp = []; $temp['symbol'] = strtoupper($explode_arr[0]) . '/' . $coin; $temp['currency'] = strtoupper($explode_arr[0]); $temp['base_currency'] = strtoupper($coin); $temp['close'] = number_format($close, 6, '.', ''); if ('0.000000' == $temp['close']) continue; $temp['close_usd'] = (float)sprintf("%0.6f", $close * $this->basic_price[$coin]['usd']); $temp['close_rmb'] = (float)sprintf("%0.4f", $close * $this->basic_price[$coin]['rmb']); $temp['change'] = (0 == $open) ? 0 : (float)sprintf("%0.2f", ($close - $open) / $open * 100); $temp['high_usd'] = (float)sprintf("%0.4f", $high * $this->basic_price[$coin]['usd']); $temp['low_usd'] = (float)sprintf("%0.4f", $low * $this->basic_price[$coin]['usd']); $temp['high_rmb'] = (float)sprintf("%0.4f", $high * $this->basic_price[$coin]['rmb']); $temp['low_rmb'] = (float)sprintf("%0.4f", $low * $this->basic_price[$coin]['rmb']); $temp['vol'] = (float)sprintf("%0.4f", $vol); $temp['optional'] = false; $temp['platform_zh'] = '币安'; $temp['platform_us'] = 'binance'; array_push($ticker, $temp); } } } $this->code = 0; $data = [ 'ticker' => $ticker, 'page' => [ 'pageSize' => 50, 'currentPage' => (int)$page, ] ]; return ['code' => $this->code, 'data' => $data]; } public function TickerSort() { $len = $this->redis->llen($this->supported_symbol_list); $ticker = []; for ($i = 0; $i < $len; $i++) { $symbol = $this->redis->lindex($this->supported_symbol_list, $i); list($close, $open) = $this->redis->hmget($this->quotation_prefix . strtolower($symbol), 'last', 'open'); $temp = []; $temp['symbol'] = strtoupper($symbol); $temp['close'] = number_format($close, 6, '.', ''); $temp['change'] = (0 == $open) ? 0 : (float)sprintf("%0.2f", ($close - $open) / $open * 100); array_push($ticker, $temp); } $ticker_sort_close= $this->arraySort($ticker,'close'); foreach ($ticker_sort_close as $val) { $this->redis->lpush($this->supported_symbol_close_sort_list, $val['symbol']); } $ticker_change_close= $this->arraySort($ticker,'change'); foreach ($ticker_change_close as $val) { $this->redis->lpush($this->supported_symbol_change_sort_list, $val['symbol']); } } protected function arraySort($array, $keys, $sort = SORT_DESC) { $keysValue = []; foreach ($array as $k => $v) { $keysValue[$k] = $v[$keys]; } array_multisort($keysValue, $sort, $array); return $array; } }