日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php 按照laravel5.5,Laravel5.5 综合使用

發布時間:2025/3/12 php 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php 按照laravel5.5,Laravel5.5 综合使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

使用 Laravel5.5 開發一個自動交割的項目,把使用到的開源擴展包及特性整理起來,以供后續使用。

一、安裝IDE提示工具

Laravel IDE Helper 是一個極其好用的代碼提示及補全工具,可以給編寫代碼帶來極大的便利。

1、安裝

# 如果只想在開發環境安裝請加上 --dev

composer require barryvdh/laravel-ide-helper

安裝 doctrine/dbal 「請裝上它,在為模型注釋字段的時候必須用到它」

# 如果只想在開發環境安裝請加上 --dev

composer require "doctrine/dbal: ~2.3"

三個常用命令

php artisan ide-helper:generate - 為 Facades 生成注釋

php artisan ide-helper:models - 為數據模型生成注釋

php artisan ide-helper:meta - 生成 PhpStorm Meta file

二、Monolog日志包

日志的重要程度不言而喻, 不管是在開發過程中, 還是部署到生產環境后, 都是經常使用的.

隨著 psr-3 的出現, 終于統一了 php 中日志的風格.但是, 好用的記錄日志系統, 也很重要.

monolog 是我遇到的最好的日志系統.而且, 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

創建命令

php artisan make:command Spider

命令參數

// concurrency為并發數 keyWords為查詢關鍵詞

protected $signature = 'command:spider {concurrency} {keyWords*}';

實戰

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

1.添加系統定時任務

在laravel中添加定時任務很簡單,首先在系統crontab 添加一個artisan的定時任務,每分鐘執行一次。

> 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, 每十分鐘調用一次

$schedule->command('test:spider-request')

->everyFifteenMinutes()->withoutOverlapping();

}

添加好了之后,我們可以直接使用這個命令測試定時任務是否可以執行:

> php /home/vagrant/Code/digcoin/artisan test:spider-request

OK,只需要簡單的兩步便可實現laravel的定時任務添加。

總結

以上是生活随笔為你收集整理的php 按照laravel5.5,Laravel5.5 综合使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。