TickerController.php 2.73 KB
<?php

namespace console\controllers;

use common\service\exchange\ExchangeFactory;
use Yii;
use linslin\yii2\curl\Curl;
use yii\console\Controller;
use voku\helper\HtmlDomParser;
use common\models\psources\CoinSupportedCurrency;
use common\service\exchange\ExchangeBuilderFactory;

class TickerController extends Controller
{
    public function actionSort()
    {
        $class = ['Binance', 'Huobi', 'Zhaobi', 'Bitnasdaq'];
        foreach ($class as $val) {
            go(function () use ($val) {
                \Co::sleep(0.5);
                $ticker_builder = ExchangeBuilderFactory::create($val);
                $ticker_builder->TickerSort();
            });
        }
        echo date('Y-m-d H:i:s') . '排序更新成功' . PHP_EOL;
        return 0;
    }

    public function actionHot()
    {
        $ticker_builder = ExchangeBuilderFactory::create('Huobi');
        $ticker_builder->hotTickerUpdate();
        echo date('Y-m-d H:i:s') . '更新成功' . PHP_EOL;
        return 0;
    }

    public function actionExchange($exchange)
    {
        $ticker_builder = ExchangeFactory::createExchange($exchange);
        $ticker_builder->setQuotation();
        echo date('Y-m-d H:i:s') . $exchange. '更新成功' . PHP_EOL;
        return 0;
    }

    public function actionCurrency()
    {
        $currency_model = CoinSupportedCurrency::find()->groupBy('currency_id')->all();
        $curl = new Curl();
        foreach ($currency_model as $val) {
            if (1111 == $val->currency->pj_id) continue;
            go(function () use ($val, $curl) {
                \Co::sleep(0.5);
                $response = $curl->setPostParams([
                    'erectDate' => '',
                    'nothing' => '',
                    'pjname' => $val->currency->pj_id
                ])->post('https://srh.bankofchina.com/search/whpj/search_cn.jsp');

                $response = iconv('UTF-8', 'GBK//TRANSLIT', $response);
                $html = HtmlDomParser::str_get_html($response);
                $div = ($html->find('div.BOC_main'));
                if ($div->find('td')) {
                    foreach ($div->find('td') as $key => $e) {
                        if ($key == 5) {
                            $key = 'quotation_boc_' . 'CNY_' . $val->currency->symbol;
                            $currency = rtrim(sprintf('%.6f', 1 / ($e->innertext / 100)), '0');
                            Yii::$app->redis_currency->hmset($key, 'low', $currency, 'high', $currency, 'last', $currency, 'open', $currency);
                        }
                    }
                }
            });
        }
        echo date('Y-m-d H:i:s') . '计价货币更新成功' . PHP_EOL;
        return 0;
    }
}