2017年12月22日17:40:03
版本5.4.X
一下是可能会遇到的坑
1,必须的写路由转发才能访问控制器,当然你可以自动路由访问,但是需要些匹配规则,其实还是转发了
2,Laravel 做计划任务的时候坑真的好多,比如不能直接跨控制器访问,web的是web的路由,console是它自己的,所以你的功能和逻辑代码必须在Repository或者service里面,不然你懂的,做好逻辑代码分离
官方文档只有用过的才能看得懂,我很无奈
完整流程
app\Console\Commands下建立你的任务文件
SpiderTask.php
<?php namespace App\Console\Commands; use Illuminate\Console\Command; //use Illuminate\Support\Facades\Redis; use App\Repositories\SpiderRepository;//具体逻辑代码 class SpiderTask extends Command { protected $taskserver; /** * The name and signature of the console command. * * @var string */ protected $signature = ‘SpiderTask‘; /** * The console command description. * * @var string */ protected $description = ‘spider every one hour crawl data‘; protected $spider; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); $this->spider = new SpiderRepository(); } /** * Execute the console command. * * @return mixed */ public function handle() { $this->spider->do_all();//具体执行地方 } }
然后注册到Kernel.php
<?php namespace App\Console; use Illuminate\Console\Scheduling\Schedule; use Illuminate\Foundation\Console\Kernel as ConsoleKernel; use App\Repositories\SpiderRepository;//我的具体逻辑代码地方 class Kernel extends ConsoleKernel { /** * The Artisan commands provided by your application. * * @var array */ protected $commands = [ // ‘App\Console\Commands\SpiderTask‘, //必须 ]; /** * Define the application‘s command schedule. * * @param \Illuminate\Console\Scheduling\Schedule $schedule * @return void */ protected function schedule(Schedule $schedule) { // $schedule->call(function () { // $Spider = new SpiderRepository(); // $Spider->do_all(); // })->daily(); $schedule->command(‘SpiderTask‘)->daily();
//两种方法都可以,建议第二种,逻辑更清晰 } /** * Register the Closure based commands for the application. * * @return void */ protected function commands() { require base_path(‘routes/console.php‘); } }
注意:
你要测试你的代码逻辑有没有错误
最好在Linux下测试,因为windows好多问题
在代码根目录,如果命令没有到全局,使用完整路径
php artisan schedule:run
everyMinute才会实时运行,可以看到报错
http://laravelacademy.org/post/6931.html 官方文档
<?php namespace App\Repositories; use App\Models\Spider; //use phpspider\core\phpspider; use phpspider\core\requests; use phpspider\core\selector; class SpiderRepository { use BaseRepository; protected $model; /** * ActivityRepository constructor. * @param Activity $activity */ public function __construct() { $this->model = new Spider(); } public function do_all() { $this->cjysjs(); $this->shysjs(); $this->nchn(); $this->ltbj(); } //长江有色金属 public function cjysjs() { $html = requests::get(‘http://www.ccmn.cn/‘); $data = selector::select($html, "#40288092327140f601327141c0560001", "css"); $data1 = selector::select($data, "tr", "css"); array_shift($data1); $array = array(); if (!empty($data1) && is_array($data1)) { foreach ($data1 as $k => &$v) { $data2 = selector::select($v, "td", "css"); foreach ($data2 as $kk => &$vv) { $vv = str_replace(‘ ‘, ‘‘, $vv); $vv = str_replace(array("\r\n", "\r", "\n"), "", $vv); $vv = trim($vv); } $data2[‘3‘] = selector::select($data2[‘3‘], "font", "css"); unset($data2[‘6‘]); $array[] = $data2; } if (empty($array)) { $info = date("Y-m-d H:i:s", time()) . ‘:长江有色金属抓取失败!‘; Log::info($info); } $name = ‘cjysjs‘; $_data = []; if (!empty($array) && is_array($array)) { $_data[‘value‘] = json_encode($array); $_data[‘crawl_time‘] = time(); $count = $this->getData($name); if (empty($count)) { //增加 $_data[‘name‘] = $name; $result = $this->saveData(null, $_data); } else { //更新 $_data[‘name‘] = $name; $result = $this->saveData($name, $_data); } } } } public function saveData($name = null, $data = null) { return $this->model->updateOrCreate([‘name‘ => $name], $data); } public function getData($name) { return $this->model->where(‘name‘, $name)->count(); } }
3,