Commit 123482d1 authored by rlgy's avatar rlgy

API规范

parent a4cf284f
......@@ -25,8 +25,9 @@ class BaseResponse extends Response
$return['code'] = 1;
$return['msg'] = '数据为空';
} else {
$return['code'] = 0;
$return['data'] = $data;
$return['code'] = 0;
$return['count'] = count($data);
$return['data'] = $data;
}
\Yii::$app->response->data = $return;
parent::send();
......
......@@ -84,6 +84,10 @@ class CoinController extends BaseController
Yii::$app->response->data = $data;
}
/**
* 单币种按照id查询
* @return array|null|\yii\db\ActiveRecord
*/
public function actionGetCoinById()
{
$request = Yii::$app->request;
......@@ -99,12 +103,11 @@ class CoinController extends BaseController
*/
public function actionGetRecList()
{
$request = Yii::$app->request;
$page = $request->post('page', 1);
$limit = $request->post('limit', 999);
$condition = [['recommend' => '1']];
$data = CoinBusiness::getApiList($page, $limit, $condition);
Yii::$app->response->data = $data;
$request = Yii::$app->request;
$page = $request->post('page', 1);
$limit = $request->post('limit', 999);
$condition = [['recommend' => '1']];
return CoinBusiness::getApiList($page, $limit, $condition);
}
/**
......@@ -120,13 +123,13 @@ class CoinController extends BaseController
if ($coin) {
$miner_fee = $coin->minerFee;
if (empty($miner_fee)) {
return ['code' => 1, 'msg' => '数据为空'];
return [];
}
} else {
//如果coin为null,$coin->minerFee会抛出Trying to get property 'minerFee' of non-object",code=>8
throw new Exception('8', '币种不存在');
}
return ['code' => 0, 'data' => $miner_fee];
return [$miner_fee];
}
......@@ -135,11 +138,15 @@ class CoinController extends BaseController
*/
public function actionCoinIndex()
{
$names = Yii::$app->request->post('names');
$condition = [['in', 'name', $names]];
Yii::$app->response->data = CoinBusiness::getApiListForIndex($condition);
$names = Yii::$app->request->post('names');
$condition = [['in', 'name', $names]];
return CoinBusiness::getApiListForIndex($condition);
}
/**
* 按照名称搜索币种
* @return array
*/
public function actionSearchCoinByName()
{
$request = Yii::$app->request;
......@@ -148,9 +155,7 @@ class CoinController extends BaseController
$limit = $request->post('limit', 10);
if ($name) {
$condition = [['or', ['like', 'name', $name], ['like', 'nickname', $name]]];
$row = Coin::getSelectList($page, $limit, ['id', 'sid', 'icon', 'name', 'nickname', 'chain'],
$condition);
return $row;
return CoinBusiness::SearchByName($page, $limit, $condition);
}
}
}
\ No newline at end of file
......@@ -9,6 +9,7 @@
namespace common\business;
use common\models\pwallet\Coin;
use common\service\coin\CoinFactory;
use common\service\CoinService;
/**
......@@ -70,20 +71,21 @@ class CoinBusiness
{
$rows = Coin::getList($page, $limit, $condition);
if ($rows['count'] > 0) {
$datas = $rows['data'];
foreach ($datas as $key => $value) {
$rows = $rows['data'];
foreach ($rows as $key => $value) {
//获取行情信息
if (strtoupper($value['name']) == 'BTY') {
$rows['data'][$key]['quotation'] = CoinService::quotationBTY();
$rows[$key]['quotation'] = CoinService::quotationBTY();
continue;
}
$rows['data'][$key]['quotation'] = CoinService::quotation($value['sid']);
$rows[$key]['quotation'] = CoinService::quotation($value['sid']);
}
}
return $rows;
}
/**
* 根据name返回币种信息
* @param array $condition 需要的币种sid列表
* @return array
*/
......@@ -93,7 +95,31 @@ class CoinBusiness
if ($rows['count'] > 0) {
$rows = $rows['data'];
foreach ($rows as $key => $row) {
$coinServer = new CoinService($row['id'], $row['sid']);
$coinServer = CoinFactory::createCoin($row['name'], $row['id'], $row['sid']);
$rows[$key]['sid'] = ucfirst($rows[$key]['sid']);
$rows[$key]['rmb'] = $coinServer->getPrice();
$rows[$key]['usd'] = $coinServer->getDollar();
$coinServer->__destruct();
}
return $rows;
}
return [];
}
/**
* 根据名称搜索
* @param int $page
* @param int $limit
* @param array $condition
* @return array|\yii\db\ActiveRecord|\yii\db\ActiveRecord[]
*/
public static function SearchByName($page = 1, $limit = 10, $condition = [])
{
$rows = Coin::getSelectList($page, $limit, ['id', 'sid', 'icon', 'name', 'nickname', 'chain'], $condition);
if ($rows['count'] > 0) {
$rows = $rows['data'];
foreach ($rows as $key => $row) {
$coinServer = CoinFactory::createCoin($row['name'], $row['id'], $row['sid']);
$rows[$key]['sid'] = ucfirst($rows[$key]['sid']);
$rows[$key]['rmb'] = $coinServer->getPrice();
$rows[$key]['usd'] = $coinServer->getDollar();
......@@ -106,6 +132,7 @@ class CoinBusiness
/**
* 获取币种的所有信息
* @return array
*/
public static function getCoinAllById($id)
{
......@@ -113,9 +140,13 @@ class CoinBusiness
if ($row) {
//TODO 获取行情
$row['quotation'] = CoinService::quotation($row['sid']);
//行情BTY
if (strtoupper($row['name']) == 'BTY') {
$row['quotation'] = CoinService::quotationBTY();
}
//TODO 获取交易所详情
$row['exchange'] = CoinService::exchange($row['sid']);
}
return $row ? $row : [];
return $row ? [$row] : [];
}
}
\ No newline at end of file
......@@ -69,7 +69,7 @@ class Coin extends BaseActiveRecord
$query = $query->andWhere($item);
}
$count = $query->count();
$data = $query->select($columns)->offset(($page - 1) * 10)->limit($limit)->asArray()->all();
$data = $query->select($columns)->offset(($page - 1) * $limit)->limit($limit)->asArray()->all();
return ['count' => $count, 'data' => $data];
}
......
<?php
/**
* Created by PhpStorm.
* User: rlgyzhcn
* Date: 18-6-19
* Time: 下午2:25
*/
namespace common\service\coin;
abstract class Coin
{
//行情信息
protected $price; //价格
protected $dollar; //价格美元
protected $btc; //价格btc
protected $high; //最高价
protected $low; //最低价
protected $change; //涨幅(跌幅)
protected $rank; //流通市值排名
protected $circulate_value_rmb; //流通市值人民币
protected $circulate_value_usd; //流通市值美元
protected $circulate_value_btc; //流通市值btc
protected $publish_count; //发行总量
protected $circulate_count; //流通总量
protected $content;//行情html数据
public $id;//币种id
public $sid;//币种sid
public $cache_key_prifx = 'quotation_coin_';
/**
* @return mixed
*/
public function getPrice()
{
return $this->price;
}
/**
* @param mixed $price
*/
public function setPrice($price)
{
$this->price = $price;
}
/**
* @return mixed
*/
public function getDollar()
{
return $this->dollar;
}
/**
* @param mixed $dollar
*/
public function setDollar($dollar)
{
$this->dollar = $dollar;
}
/**
* @return mixed
*/
public function getBtc()
{
return $this->btc;
}
/**
* @param mixed $btc
*/
public function setBtc($btc)
{
$this->btc = $btc;
}
/**
* @return mixed
*/
public function getHigh()
{
return $this->high;
}
/**
* @param mixed $high
*/
public function setHigh($high)
{
$this->high = $high;
}
/**
* @return mixed
*/
public function getLow()
{
return $this->low;
}
/**
* @param mixed $low
*/
public function setLow($low)
{
$this->low = $low;
}
/**
* @return mixed
*/
public function getChange()
{
return $this->change;
}
/**
* @param mixed $change
*/
public function setChange($change)
{
$this->change = $change;
}
/**
* @return mixed
*/
public function getRank()
{
return $this->rank;
}
/**
* @param mixed $rank
*/
public function setRank($rank)
{
$this->rank = $rank;
}
/**
* @return mixed
*/
public function getCirculateValueRmb()
{
return $this->circulate_value_rmb;
}
/**
* @param mixed $circulate_value_rmb
*/
public function setCirculateValueRmb($circulate_value_rmb)
{
$this->circulate_value_rmb = $circulate_value_rmb;
}
/**
* @return mixed
*/
public function getCirculateValueUsd()
{
return $this->circulate_value_usd;
}
/**
* @param mixed $circulate_value_usd
*/
public function setCirculateValueUsd($circulate_value_usd)
{
$this->circulate_value_usd = $circulate_value_usd;
}
/**
* @return mixed
*/
public function getCirculateValueBtc()
{
return $this->circulate_value_btc;
}
/**
* @param mixed $circulate_value_btc
*/
public function setCirculateValueBtc($circulate_value_btc)
{
$this->circulate_value_btc = $circulate_value_btc;
}
/**
* @return mixed
*/
public function getPublishCount()
{
return $this->publish_count;
}
/**
* @param mixed $publish_count
*/
public function setPublishCount($publish_count)
{
$this->publish_count = $publish_count;
}
/**
* @return mixed
*/
public function getCirculateCount()
{
return $this->circulate_count;
}
/**
* @param mixed $circulate_count
*/
public function setCirculateCount($circulate_count)
{
$this->circulate_count = $circulate_count;
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: rlgyzhcn
* Date: 18-6-19
* Time: 下午2:20
*/
namespace common\service\coin;
class CoinBTYService extends Coin implements CoinInterface
{
public function __construct($id, $sid)
{
}
public function init()
{
// TODO: Implement init() method.
}
public function __destruct()
{
// TODO: Implement __destruct() method.
}
public function getPrice()
{
return parent::getPrice(); // TODO: Change the autogenerated stub
}
public function setPrice($price)
{
parent::setPrice($price); // TODO: Change the autogenerated stub
}
public function getDollar()
{
return parent::getDollar(); // TODO: Change the autogenerated stub
}
public function setDollar($dollar)
{
parent::setDollar($dollar); // TODO: Change the autogenerated stub
}
public function getBtc()
{
return parent::getBtc(); // TODO: Change the autogenerated stub
}
public function setBtc($btc)
{
parent::setBtc($btc); // TODO: Change the autogenerated stub
}
public function getHigh()
{
return parent::getHigh(); // TODO: Change the autogenerated stub
}
public function setHigh($high)
{
parent::setHigh($high); // TODO: Change the autogenerated stub
}
public function getLow()
{
return parent::getLow(); // TODO: Change the autogenerated stub
}
public function setLow($low)
{
parent::setLow($low); // TODO: Change the autogenerated stub
}
public function getChange()
{
return parent::getChange(); // TODO: Change the autogenerated stub
}
public function setChange($change)
{
parent::setChange($change); // TODO: Change the autogenerated stub
}
public function getRank()
{
return parent::getRank(); // TODO: Change the autogenerated stub
}
public function setRank($rank)
{
parent::setRank($rank); // TODO: Change the autogenerated stub
}
public function getCirculateValueRmb()
{
return parent::getCirculateValueRmb(); // TODO: Change the autogenerated stub
}
public function setCirculateValueRmb($circulate_value_rmb)
{
parent::setCirculateValueRmb($circulate_value_rmb); // TODO: Change the autogenerated stub
}
public function getCirculateValueUsd()
{
return parent::getCirculateValueUsd(); // TODO: Change the autogenerated stub
}
public function setCirculateValueUsd($circulate_value_usd)
{
parent::setCirculateValueUsd($circulate_value_usd); // TODO: Change the autogenerated stub
}
public function getCirculateValueBtc()
{
return parent::getCirculateValueBtc(); // TODO: Change the autogenerated stub
}
public function setCirculateValueBtc($circulate_value_btc)
{
parent::setCirculateValueBtc($circulate_value_btc); // TODO: Change the autogenerated stub
}
public function getPublishCount()
{
return parent::getPublishCount(); // TODO: Change the autogenerated stub
}
public function setPublishCount($publish_count)
{
parent::setPublishCount($publish_count); // TODO: Change the autogenerated stub
}
public function getCirculateCount()
{
return parent::getCirculateCount(); // TODO: Change the autogenerated stub
}
public function setCirculateCount($circulate_count)
{
parent::setCirculateCount($circulate_count); // TODO: Change the autogenerated stub
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: rlgyzhcn
* Date: 18-6-19
* Time: 下午1:56
*/
namespace common\service\coin;
class CoinFactory
{
public static function createCoin($coinName = '', $id, $sid)
{
$coinName = strtoupper($coinName);
$className = __NAMESPACE__ . '\Coin' . $coinName . 'Service';
if (class_exists($className)) {
return new $className($id, $sid);
} else {
return new CoinService($id, $sid);
}
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: rlgyzhcn
* Date: 18-6-19
* Time: 下午2:12
*/
namespace common\service\coin;
interface CoinInterface
{
//币种实例化接口
public function __construct($id, $sid);
//币种初始化
public function init();
//币种销毁
public function __destruct();
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: rlgyzhcn
* Date: 18-6-5
* Time: 上午11:17
*/
namespace common\service\coin;
use Yii;
use common\helpers\Curl;
/**
* 币种服务类
* Class CoinService
* @package common\service
*/
class CoinService extends Coin implements CoinInterface
{
public function __construct($id, $sid)
{
$this->id = $id;
$this->sid = $sid;
}
public function __destruct()
{
unset($this->content);
}
public function init()
{
$url = Yii::$app->params['feixiaohao_domain'] . Yii::$app->params['feixiaohao_page']['currencies'] . $this->sid . '/';
$ch = new Curl();
$content = $ch->get($url, true);
$this->content = $content;
}
/**
* @return mixed
*/
public function getPrice()
{
$result = Yii::$app->cache->get($this->cache_key_prifx . $this->sid . '_price');
if ($result === false) {
if (empty($this->content)) {
$this->init();
}
$pattern = '/<div class=coinprice>(.*?)<span class=(tags-red|tags-green)>(.*?)<\/span><\/div>/is';
preg_match_all($pattern, $this->content, $matchs, PREG_SET_ORDER);
$this->setPrice($matchs[0][1]);
Yii::$app->cache->set($this->cache_key_prifx . $this->sid . '_price', $this->price,
Yii::$app->params['curl_cache_time']['quotation']);
} else {
$this->setPrice($result);
}
return $this->price;
}
/**
* @param mixed $price
*/
public function setPrice($price)
{
$this->price = $price;
}
/**
* @return mixed
*/
public function getDollar()
{
$result = Yii::$app->cache->get($this->cache_key_prifx . $this->sid . '_usd');
if ($result === false) {
if (empty($this->content)) {
$this->init();
}
preg_match_all('/<div class=sub><span>(.*?)<\/span>(.*?)<span>(.*?)<\/span><\/div>/is', $this->content,
$matchs,
PREG_SET_ORDER);
$this->setDollar($matchs[0][1]);
Yii::$app->cache->set($this->cache_key_prifx . $this->sid . '_usd', $this->dollar,
Yii::$app->params['curl_cache_time']['quotation']);
} else {
$this->setDollar($result);
}
return $this->dollar;
}
/**
* @param mixed $dollar
*/
public function setDollar($dollar)
{
$this->dollar = $dollar;
}
/**
* @return mixed
*/
public function getBtc()
{
$result = Yii::$app->cache->get($this->cache_key_prifx . $this->sid . '_btc');
if ($result === false) {
if (empty($this->content)) {
$this->init();
}
preg_match_all('/<div class=sub><span>(.*?)<\/span>(.*?)<span>(.*?)<\/span><\/div>/is', $this->content,
$matchs,
PREG_SET_ORDER);
$this->setBtc($matchs[0][3]);
Yii::$app->cache->set($this->cache_key_prifx . $this->sid . '_btc', $this->btc,
Yii::$app->params['curl_cache_time']['quotation']);
} else {
$this->setBtc($result);
}
return $this->btc;
}
/**
* @param mixed $btc
*/
public function setBtc($btc)
{
$this->btc = $btc;
}
/**
* @return mixed
*/
public function getHigh()
{
$result = Yii::$app->cache->get($this->cache_key_prifx . $this->sid . '_high');
if ($result === false) {
if (empty($this->content)) {
$this->init();
}
preg_match_all('/<div class=lowHeight>(.*?)<\/div><div class=sub>/is', $$this->content, $matchs);
preg_match_all('/<span class=value>(.*?)<\/span>/is', $matchs[1][0], $matchs);
$this->setHigh($matchs[1][0]);
Yii::$app->cache->set($this->cache_key_prifx . $this->sid . '_high', $this->high,
Yii::$app->params['curl_cache_time']['quotation']);
} else {
$this->setBtc($result);
}
return $this->high;
}
/**
* @param mixed $high
*/
public function setHigh($high)
{
$this->high = $high;
}
/**
* @return mixed
*/
public function getLow()
{
$result = Yii::$app->cache->get($this->cache_key_prifx . $this->sid . '_low');
if ($result === false) {
if (empty($this->content)) {
$this->init();
}
preg_match_all('/<div class=lowHeight>(.*?)<\/div><div class=sub>/is', $$this->content, $matchs);
preg_match_all('/<span class=value>(.*?)<\/span>/is', $matchs[1][0], $matchs);
$this->setLow($matchs[1][1]);
Yii::$app->cache->set($this->cache_key_prifx . $this->sid . '_low', $this->low,
Yii::$app->params['curl_cache_time']['quotation']);
} else {
$this->setLow($result);
}
return $this->low;
}
/**
* @param mixed $low
*/
public function setLow($low)
{
$this->low = $low;
}
/**
* @return mixed
*/
public function getChange()
{
$result = Yii::$app->cache->get($this->cache_key_prifx . $this->sid . '_change');
if ($result === false) {
if (empty($this->content)) {
$this->init();
}
$pattern = '/<div class=coinprice>(.*?)<span class=(tags-red|tags-green)>(.*?)<\/span><\/div>/is';
preg_match_all($pattern, $this->content, $matchs, PREG_SET_ORDER);
$this->setChange($matchs[0][3]);
Yii::$app->cache->set($this->cache_key_prifx . $this->sid . '_change', $this->change,
Yii::$app->params['curl_cache_time']['quotation']);
} else {
$this->setChange($result);
}
return $this->change;
}
/**
* @param mixed $change
*/
public function setChange($change)
{
$this->change = $change;
}
/**
* @return mixed
*/
public function getRank()
{
$result = Yii::$app->cache->get($this->cache_key_prifx . $this->sid . '_rank');
if ($result === false) {
if (empty($this->content)) {
$this->init();
}
$pattern = '/<div class=value>(.*?)<span class=tag-marketcap>(.*?)<\/span><\/div><div class=sub>(.*?)<\/div><div class=sub>(.*?)<\/div>/is';
preg_match_all($pattern, $$this->content, $matchs, PREG_SET_ORDER);
$this->setRank($matchs[0][2]);
Yii::$app->cache->set($this->cache_key_prifx . $this->sid . '_rank', $this->rank,
Yii::$app->params['curl_cache_time']['quotation']);
} else {
$this->setRank($result);
}
return $this->rank;
}
/**
* @param mixed $rank
*/
public function setRank($rank)
{
$this->rank = $rank;
}
/**
* @return mixed
*/
public function getCirculateValueRmb()
{
$result = Yii::$app->cache->get($this->cache_key_prifx . $this->sid . '_circulate_value_rmb');
if ($result === false) {
if (empty($this->content)) {
$this->init();
}
$pattern = '/<div class=value>(.*?)<span class=tag-marketcap>(.*?)<\/span><\/div><div class=sub>(.*?)<\/div><div class=sub>(.*?)<\/div>/is';
preg_match_all($pattern, $$this->content, $matchs, PREG_SET_ORDER);
$this->setCirculateValueRmb($matchs[0][1]);
Yii::$app->cache->set($this->cache_key_prifx . $this->sid . '_circulate_value_rmb',
$this->circulate_value_rmb,
Yii::$app->params['curl_cache_time']['quotation']);
} else {
$this->setCirculateValueRmb($result);
}
return $this->circulate_value_rmb;
}
/**
* @param mixed $circulate_value_rmb
*/
public function setCirculateValueRmb($circulate_value_rmb)
{
$this->circulate_value_rmb = $circulate_value_rmb;
}
/**
* @return mixed
*/
public function getCirculateValueUsd()
{
$result = Yii::$app->cache->get($this->cache_key_prifx . $this->sid . '_circulate_value_usd');
if ($result === false) {
if (empty($this->content)) {
$this->init();
}
$pattern = '/<div class=value>(.*?)<span class=tag-marketcap>(.*?)<\/span><\/div><div class=sub>(.*?)<\/div><div class=sub>(.*?)<\/div>/is';
preg_match_all($pattern, $$this->content, $matchs, PREG_SET_ORDER);
$this->setCirculateValueUsd($matchs[0][3]);
Yii::$app->cache->set($this->cache_key_prifx . $this->sid . '_circulate_value_usd',
$this->circulate_value_usd,
Yii::$app->params['curl_cache_time']['quotation']);
} else {
$this->setCirculateValueUsd($result);
}
return $this->circulate_value_usd;
}
/**
* @param mixed $circulate_value_usd
*/
public function setCirculateValueUsd($circulate_value_usd)
{
$this->circulate_value_usd = $circulate_value_usd;
}
/**
* @return mixed
*/
public function getCirculateValueBtc()
{
$result = Yii::$app->cache->get($this->cache_key_prifx . $this->sid . '_circulate_value_btc');
if ($result === false) {
if (empty($this->content)) {
$this->init();
}
$pattern = '/<div class=value>(.*?)<span class=tag-marketcap>(.*?)<\/span><\/div><div class=sub>(.*?)<\/div><div class=sub>(.*?)<\/div>/is';
preg_match_all($pattern, $$this->content, $matchs, PREG_SET_ORDER);
$this->setCirculateValueBtc($matchs[0][4]);
Yii::$app->cache->set($this->cache_key_prifx . $this->sid . '_circulate_value_btc',
$this->circulate_value_btc,
Yii::$app->params['curl_cache_time']['quotation']);
} else {
$this->setCirculateValueBtc($result);
}
return $this->circulate_value_btc;
}
/**
* @param mixed $circulate_value_btc
*/
public function setCirculateValueBtc($circulate_value_btc)
{
$this->circulate_value_btc = $circulate_value_btc;
}
/**
* @return mixed
*/
public function getPublishCount()
{
$result = Yii::$app->cache->get($this->cache_key_prifx . $this->sid . '_publish_count');
if ($result === false) {
if (empty($this->content)) {
$this->init();
}
$pattern = '/<div class=tit>流通量<\/div><div class=value>(.*?)<\/div><div class=tit>总发行量<\/div><div class=value>(.*?)<\/div>/is';
preg_match_all($pattern, $$this->content, $matchs, PREG_SET_ORDER);
$this->setPublishCount($matchs[0][2]);
Yii::$app->cache->set($this->cache_key_prifx . $this->sid . '_publish_count',
$this->publish_count,
Yii::$app->params['curl_cache_time']['quotation']);
} else {
$this->setPublishCount($result);
}
return $this->publish_count;
}
/**
* @param mixed $publish_count
*/
public function setPublishCount($publish_count)
{
$this->publish_count = $publish_count;
}
/**
* @return mixed
*/
public function getCirculateCount()
{
$result = Yii::$app->cache->get($this->cache_key_prifx . $this->sid . '_circulate_count');
if ($result === false) {
if (empty($this->content)) {
$this->init();
}
$pattern = '/<div class=tit>流通量<\/div><div class=value>(.*?)<\/div><div class=tit>总发行量<\/div><div class=value>(.*?)<\/div>/is';
preg_match_all($pattern, $$this->content, $matchs, PREG_SET_ORDER);
$this->setCirculateCount($matchs[0][1]);
Yii::$app->cache->set($this->cache_key_prifx . $this->sid . '_publish_count',
$this->circulate_count,
Yii::$app->params['curl_cache_time']['quotation']);
} else {
$this->setCirculateCount($result);
}
return $this->circulate_count;
}
/**
* @param mixed $circulate_count
*/
public function setCirculateCount($circulate_count)
{
$this->circulate_count = $circulate_count;
}
/**
* 爬取交易所列表,默认eos
*
* @param $name string 币种名称
* @return array
*/
public static function exchange($name = 'eos')
{
//缓存数据
$key = [
__CLASS__,
__METHOD__,
'coin',//表名
$name,//sid
];
$result = Yii::$app->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("/<div class=boxContain><table class=table3 id=markets>(.*)?<\/table>/is", $content,
$matchs);
$content = $matchs[1][0];
preg_match_all('/<a href="(.*?)"(.*?)alt=(.*?)>/is', $content, $matchs);
//匹配到了
//保存匹配数据
$count = count($matchs[1]);
$result = [];
$r = [];
for ($i = 0; $i < $count; $i++) {
if (!in_array($matchs[3][$i], $r)) {
$r[] = $matchs[3][$i];
//爬取正确的官网
if (strpos($matchs[1][$i], '/exchange') === false) {
$matchs[1][$i] .= 'https:';
} else {
$url = 'https://www.feixiaohao.com' . $matchs[1][$i];
$content = $ch->get($url, true);
preg_match_all('/官网地址:<a(.*?)>(.*?)<\/a><\/span>/', $content, $matchs2);
$matchs[1][$i] = $matchs2[2][0];
}
$result[] = ['url' => $matchs[1][$i], 'name' => $matchs[3][$i]];
}
}
unset($content, $i, $r, $url, $matchs2);
Yii::$app->cache->set($key, $result, Yii::$app->params['curl_cache_time']['exchange']);//set caching
return ['count' => count($result), 'data' => $result];
} catch (\Exception $exception) {
return ['count' => 0, 'data' => []];
}
}
return ['count' => count($result), 'data' => $result];
}
/**
* 爬取交易所数量
* @param string $sid
* @return integer|bool
*/
public static function exchange_count($sid = 'eos')
{
$key = [
__CLASS__,
__METHOD__,
'coin',//表名
$sid,//sid
];
$result = Yii::$app->cache->get($key);
if (!$result) {
$url = Yii::$app->params['feixiaohao_domain'] . Yii::$app->params['feixiaohao_page']['coinmarket'] . $sid . '/';
$ch = new Curl();
try {
$content = $ch->get($url, true);
preg_match_all("/<div class=boxContain><table class=table3 id=markets>(.*)?<\/table>/is", $content,
$matchs);
$content = $matchs[1][0];
preg_match_all('/alt=(.*?)>/is', $content, $matchs);
//匹配到了
$result = array_unique($matchs[1]);
$result = count($result);
unset($content, $matchs2);
Yii::$app->cache->set($key, $result,
Yii::$app->params['curl_cache_time']['exchange_count']);//set caching
} catch (\Exception $exception) {
}
}
return $result ? $result : 0;
}
/**
* 爬取币种的实时行情,默认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 = '/<div class=coinprice>(.*?)<span class=(tags-red|tags-green)>(.*?)<\/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 class=lowHeight>(.*?)<\/div><div class=sub>/is', $content, $matchs);
preg_match_all('/<span class=value>(.*?)<\/span>/is', $matchs[1][0], $matchs);
$result['high'] = $matchs[1][0];
$result['low'] = $matchs[1][1];
//美元 btc
preg_match_all('/<div class=sub><span>(.*?)<\/span>(.*?)<span>(.*?)<\/span><\/div>/is', $content,
$matchs,
PREG_SET_ORDER);
$result['dollar'] = $matchs[0][1];
$result['btc'] = $matchs[0][3];
//爬取市值与排名
$pattern = '/<div class=value>(.*?)<span class=tag-marketcap>(.*?)<\/span><\/div><div class=sub>(.*?)<\/div><div class=sub>(.*?)<\/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 class=tit>流通量<\/div><div class=value>(.*?)<\/div><div class=tit>总发行量<\/div><div class=value>(.*?)<\/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, Yii::$app->params['curl_cache_time']['quotation']);//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://kdata.zhaobi.com:4062/kdata?datafile=db&c=BTYUSDT&p=H1&action=init&count=24&ind=volumes&out=json');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$content = curl_exec($ch);
$content = json_decode($content, true);
$content = $content['main']['y'];
$min = 10e9;
$max = 0;
foreach ($content as $item) {
for ($i = 0; $i < 4; $i++) {
$min = min($min, $item[$i]);
$max = max($max, $item[$i]);
}
}
$change = ($content[23][3] - $content[0][0]) / $content[0][0] * 100;
$result['dollar'] = $content[23][3];
$result['low'] = $min;
$result['high'] = $max;
$result['change'] = sprintf("%0.2f%%", $change);
return $result;
}
}
\ 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