php 按照laravel5.5,Laravel5.5 综合使用
使用 Laravel5.5 開發(fā)一個自動交割的項目,把使用到的開源擴展包及特性整理起來,以供后續(xù)使用。
一、安裝IDE提示工具
Laravel IDE Helper 是一個極其好用的代碼提示及補全工具,可以給編寫代碼帶來極大的便利。
1、安裝
# 如果只想在開發(fā)環(huán)境安裝請加上 --dev
composer require barryvdh/laravel-ide-helper
安裝 doctrine/dbal 「請裝上它,在為模型注釋字段的時候必須用到它」
# 如果只想在開發(fā)環(huán)境安裝請加上 --dev
composer require "doctrine/dbal: ~2.3"
三個常用命令
php artisan ide-helper:generate - 為 Facades 生成注釋
php artisan ide-helper:models - 為數(shù)據(jù)模型生成注釋
php artisan ide-helper:meta - 生成 PhpStorm Meta file
二、Monolog日志包
日志的重要程度不言而喻, 不管是在開發(fā)過程中, 還是部署到生產(chǎn)環(huán)境后, 都是經(jīng)常使用的.
隨著 psr-3 的出現(xiàn), 終于統(tǒng)一了 php 中日志的風格.但是, 好用的記錄日志系統(tǒng), 也很重要.
monolog 是我遇到的最好的日志系統(tǒng).而且, laravel 中也是用的 monolog。
安裝
composer require monolog/monolog
用法
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安裝
Goutte 是一個用來解析HTML文檔的第三方庫,可以通過composer安裝
安裝
composer require fabpot/goutte
composer require guzzlehttp/guzzle
創(chuàng)建命令
php artisan make:command Spider
命令參數(shù)
// concurrency為并發(fā)數(shù) keyWords為查詢關鍵詞
protected $signature = 'command:spider {concurrency} {keyWords*}';
實戰(zhàn)
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; // 同時并發(fā)抓取
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為并發(fā)數(shù) 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()
{
// 實例化一個日志實例, 參數(shù)是 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();
},
]);
// 開始發(fā)送請求
$promise = $pool->promise();
$promise->wait();
}
public function countedAndCheckEnded()
{
if ($this->counter < $this->totalPageCount){
$this->counter++;
return;
}
$this->info("請求結(jié)束!");
}
// 運行命令:php artisan test:spider-request
}
> php artisan test:spider-request
四、定時任務
CRON是一個守護進程,它駐留在你的linux服務器中,大部分時間都沒有喚醒,但是每一分鐘它都會睜開雙眼,看看是否運行任何給定的任務,你使用crontab文件與該守護進程通信,在大多數(shù)常見的設置文件可以位于/etc/crontab,crontab文件可能看起來像這樣:
0 0 1 * * /home/full-backup
0 0 * * * /home/partial-backup
30 5 10 * * /home/check-subscriptions
1.添加系統(tǒng)定時任務
在laravel中添加定時任務很簡單,首先在系統(tǒng)crontab 添加一個artisan的定時任務,每分鐘執(zhí)行一次。
> crontab -e
// /home/vagrant/Code/digcoin/ laravel項目在服務器的地址
* * * * * php /home/vagrant/Code/digcoin/artisan schedule:run >> /dev/null 2>&1
2.項目中添加定時命令
在 App\Console\Kernel 類的 schedule 方法中定義預定的命令:
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')
// ->hourly();
// php artisan test:spider-request, 每十分鐘調(diào)用一次
$schedule->command('test:spider-request')
->everyFifteenMinutes()->withoutOverlapping();
}
添加好了之后,我們可以直接使用這個命令測試定時任務是否可以執(zhí)行:
> php /home/vagrant/Code/digcoin/artisan test:spider-request
OK,只需要簡單的兩步便可實現(xiàn)laravel的定時任務添加。
總結(jié)
以上是生活随笔為你收集整理的php 按照laravel5.5,Laravel5.5 综合使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C/C++二叉树前序遍历,中序遍历,后序
- 下一篇: 0018计算机基础知识,0018 001