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 */ public static function quotation($sid = 'eos') { $url = Yii::$app->params['feixiaohao_domain'] . Yii::$app->params['feixiaohao_page']['currencies'] . $sid . '/'; $ch = new Curl(); try { $content = $ch->get($url, true); //价格 价格美元 价格btc 最高价 最低价 涨幅(跌幅) $result = ['price' => '', 'dollar' => '', 'btc' => '', 'high' => '', 'low' => '', 'change' => '']; //价格与涨幅 //价格与涨幅 $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]; $result = array_map(function ($value) { return trim($value); }, $result); return $result; } catch (Exception $exception) { return false; } } }