cache->get($key); if ($result === false) { $url = Yii::$app->params['feixiaohao_domain'] . Yii::$app->params['feixiaohao_page']['coinmarket'] . $name . '/'; $ch = new Curl(); try { $content = $ch->get($url, true); preg_match_all("/
(.*)?<\/table>/is", $content, $matchs); $content = $matchs[1][0]; preg_match_all('/alt=(.*?)>/is', $content, $matchs); //匹配到了 $result = array_unique($matchs[1]); Yii::$app->cache->set($key, $result, 180);//set caching return ['count' => count($result), 'data' => $result]; } catch (\Exception $exception) { return ['count' => 0, 'data' => []]; } } return ['count' => count($result), 'data' => $result]; } /** * 爬取币种的实时行情,默认eos * * @param string $sid * @return object|string */ public static function quotation($sid = 'eos') { //使用缓存 $key = [ __CLASS__, __METHOD__, 'coin',//表名 $sid,//sid ]; $result = Yii::$app->cache->get($key); if ($result === false) { $url = Yii::$app->params['feixiaohao_domain'] . Yii::$app->params['feixiaohao_page']['currencies'] . $sid . '/'; $ch = new Curl(); try { $content = $ch->get($url, true); $result = [ 'price' => '',//价格 'dollar' => '',//价格美元 'btc' => '',//价格btc 'high' => '',//最高价 'low' => '',//最低价 'change' => '',//涨幅(跌幅) 'rank' => '',//流通市值排名 'circulate_value_rmb' => '',//流通市值人民币 'circulate_value_usd' => '',//流通市值美元 'circulate_value_btc' => '',//流通市值btc 'publish_count' => '',//发行总量 'circulate_count' => '',//流通总量 ]; //价格与涨幅 $pattern = '/
(.*?)(.*?)<\/span><\/div>/is'; preg_match_all($pattern, $content, $matchs, PREG_SET_ORDER); $result['price'] = $matchs[0][1]; $result['change'] = $matchs[0][3]; //最高最低值 preg_match_all('/
(.*?)<\/div>
/is', $content, $matchs); preg_match_all('/(.*?)<\/span>/is', $matchs[1][0], $matchs); $result['high'] = $matchs[1][0]; $result['low'] = $matchs[1][1]; //美元 btc preg_match_all('/
(.*?)<\/span>(.*?)(.*?)<\/span><\/div>/is', $content, $matchs, PREG_SET_ORDER); $result['dollar'] = $matchs[0][1]; $result['btc'] = $matchs[0][3]; //爬取市值与排名 $pattern = '/
(.*?)(.*?)<\/span><\/div>
(.*?)<\/div>
(.*?)<\/div>/is'; preg_match_all($pattern, $content, $matches, PREG_SET_ORDER); $result['circulate_value_rmb'] = $matches[0][1]; $result['rank'] = $matches[0][2]; $result['circulate_value_usd'] = $matches[0][3]; $result['circulate_value_btc'] = $matches[0][4]; //爬取流通总量,发行总量 $pattern = '/
流通量<\/div>
(.*?)<\/div>
总发行量<\/div>
(.*?)<\/div>/is'; preg_match_all($pattern, $content, $matches, PREG_SET_ORDER); $result['circulate_count'] = $matches[0][1]; $result['publish_count'] = $matches[0][2]; $result = array_map(function ($value) { return trim($value); }, $result); Yii::$app->cache->set($key, $result, 60);//set cache return $result; } catch (\Exception $exception) { return new \stdClass(); } } return $result; } /** * 快速修复方法,之后再改 */ public static function quotationBTY() { $result = [ 'price' => '',//价格 'dollar' => '',//价格美元 'btc' => '',//价格btc 'high' => '',//最高价 'low' => '',//最低价 'change' => '',//涨幅(跌幅) 'rank' => '',//流通市值排名 'circulate_value_rmb' => '',//流通市值人民币 'circulate_value_usd' => '',//流通市值美元 'circulate_value_btc' => '',//流通市值btc 'publish_count' => '',//发行总量 'circulate_count' => '',//流通总量 ]; $ch = curl_init('https://api2.mytoken.org/currency/currencydetail?com_id=bty_CNY&market_id=1303&market_name=cmc&symbol=BTY&anchor=CNY×tamp=1528772807124&code=5384ef255744748f1a469b6514dad6ed&v=1.4.0&platform=m&language=zh_CN&'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); $content = curl_exec($ch); $content = json_decode($content, true); if ($content['code'] == 0) { $content = $content['data']; $result['rank'] = '第' . $content['rank'] . '名'; $result['price'] = $content['price_display']; $result['dollar'] = sprintf("$%0.2f", $content['price_usd']); $result['change'] = sprintf("%0.2f%%", $content['percent_change_display']); } return $result; } }