使用 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