使用 Laravel5.5 開發一個自動交割的項目,把使用到的開源擴展包及特性整理起來,以供後續使用。
Laravel IDE Helper 是一個極其好用的代碼提示及補全工具,能夠給編寫代碼帶來極大的便利。php
# 若是隻想在開發環境安裝請加上 --dev composer require barryvdh/laravel-ide-helper
安裝 doctrine/dbal 「請裝上它,在爲模型註釋字段的時候必須用到它」html
# 若是隻想在開發環境安裝請加上 --dev composer require "doctrine/dbal: ~2.3"
詳細安裝方法,請參考這篇博文: Laravel 超好用代碼提示工具 Laravel IDE Helperlinux
三個經常使用命令laravel
- php artisan ide-helper:generate - 爲 Facades 生成註釋
- php artisan ide-helper:models - 爲數據模型生成註釋
- php artisan ide-helper:meta - 生成 PhpStorm Meta file
日誌的重要程度不言而喻, 無論是在開發過程當中, 仍是部署到生產環境後, 都是常常使用的.
隨着 psr-3
的出現, 終於統一了 php
中日誌的風格.可是, 好用的記錄日誌系統, 也很重要.monolog
是我遇到的最好的日誌系統.並且, laravel 中也是用的 monolog
。git
composer require monolog/monolog
Github地址:monolog/monologgithub
<?php use Monolog\Logger; use Monolog\Handler\StreamHandler; // create a log channel $log = new Logger('name'); $log->pushHandler(new StreamHandler('path/to/your.log', Logger::WARNING)); // $logger->pushHandler(new StreamHandler(storage_path() . '/logs/spider.log')); // add records to the log $log->warning('Foo'); $log->error('Bar');
Guzzle 是一個十分強大的php的模擬HTTP client的第三方庫,能夠經過composer安裝promise
Goutte 是一個用來解析HTML文檔的第三方庫,能夠經過composer安裝bash
composer require fabpot/goutte composer require guzzlehttp/guzzle
php artisan make:command Spider
// concurrency爲併發數 keyWords爲查詢關鍵詞 protected $signature = 'command:spider {concurrency} {keyWords*}';
<?php namespace App\Console\Commands; use Goutte\Client as GoutteClient; use GuzzleHttp\Client as GuzzleClient; use GuzzleHttp\Pool; use Illuminate\Console\Command; use Monolog\Logger; use Monolog\Handler\StreamHandler; class Spider extends Command { private $totalPageCount; private $counter = 1; private $concurrency = 7; // 同時併發抓取 private $logger = null; private $urls = [ 'https://www.feixiaohao.com/currencies/bitcoin/', // BTC 'https://www.feixiaohao.com/currencies/decred/', // DCR ]; /** * The name and signature of the console command. * * @var string */ protected $signature = 'test:spider-request'; //concurrency爲併發數 keyWords爲查詢關鍵詞 /** * The console command description. * * @var string */ protected $description = 'php spider'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function handle() { // 實例化一個日誌實例, 參數是 channel name $logger = new Logger('spider'); $logger->pushHandler(new StreamHandler(storage_path() . '/logs/spider.log')); $this->totalPageCount = count($this->urls); $guzzleClent = new GuzzleClient(); $client = new GoutteClient(); $client->setClient($guzzleClent); $request = function ($total) use ($client){ foreach ($this->urls as $url){ yield function () use($client, $url){ return $client->request('GET',$url); }; } }; // @DOC http://docs.guzzlephp.org/en/stable/quickstart.html?highlight=pool // /Users/kaiyiwang/Code/digcoin/vendor/symfony/dom-crawler/Crawler.php $pool = new Pool($guzzleClent,$request($this->totalPageCount), [ 'concurrency' => $this->concurrency, 'fulfilled' => function ($response, $index) use ($logger){ $res = $response->html(); // print_r($res); $logger->info($res); $this->info("請求第 $index 個請求,鏈接 " . $this->urls[$index]); $this->countedAndCheckEnded(); }, 'rejected' => function ($reason, $index){ $this->error("rejected" ); $this->error("rejected reason: " . $reason ); $this->countedAndCheckEnded(); }, ]); // 開始發送請求 $promise = $pool->promise(); $promise->wait(); } public function countedAndCheckEnded() { if ($this->counter < $this->totalPageCount){ $this->counter++; return; } $this->info("請求結束!"); } // 運行命令:php artisan test:spider-request }
> php artisan test:spider-request
CRON是一個守護進程,它駐留在你的linux服務器中,大部分時間都沒有喚醒,可是每一分鐘它都會睜開雙眼,看看是否運行任何給定的任務,你使用crontab文件與該守護進程通訊,在大多數常見的設置文件能夠位於/etc/crontab
,crontab文件可能看起來像這樣:服務器
0 0 1 * * /home/full-backup 0 0 * * * /home/partial-backup 30 5 10 * * /home/check-subscriptions
在laravel中添加定時任務很簡單,首先在系統crontab 添加一個artisan的定時任務,每分鐘執行一次。併發
> crontab -e // /home/vagrant/Code/digcoin/ laravel項目在服務器的地址 * * * * * php /home/vagrant/Code/digcoin/artisan schedule:run >> /dev/null 2>&1
在 App\Console\Kernel
類的 schedule
方法中定義預約的命令:
protected function schedule(Schedule $schedule) { // $schedule->command('inspire') // ->hourly(); // php artisan test:spider-request, 每十分鐘調用一次 $schedule->command('test:spider-request') ->everyFifteenMinutes()->withoutOverlapping(); }
添加好了以後,咱們能夠直接使用這個命令測試定時任務是否能夠執行:
> php /home/vagrant/Code/digcoin/artisan test:spider-request
OK,只須要簡單的兩步即可實現laravel的定時任務添加。
更多關於Laravel的任務調度,請看考該文:Laravel 的任務調度(計劃任務)功能 Task Scheduling