laravel php artisan console 二. 附加参数与案例
2016-11-28 16:35
435 查看
<?php /** * 抓取快照生成工具 */ namespace App\Modules\XXXX\Commands; use Illuminate\Console\Command; use Illuminate\Support\Facades\DB; class RunSnapshotCommand extends Command { /** * The name and signature of the console command. *调用list显示命令的时候会被用到 * @var string */ protected $signature = 'huiliu:snapshot {--table_name=} {--start=}{--end=}'; /** * The console command description. *调用list显示命令的时候会被用到 * @var string */ protected $description = "抓百度快照!"; /** * @param string $table_name 表名 * @param int $start 开始id 默认:0 * @param int $end 结束id 默认:最大id */ public function handle() { $table_name = $this->option('table_name'); $start = empty($this->option('start') ) ? 0 : $this->option('start') ; $max = DB::table($table_name)->count(); $end = empty($this->option('end') ) ? $max : $this->option('end') ; DB::beginTransaction(); try { DB::table($table_name)->select('snapshot', 'url', 'id')->whereBetween('id', [$start,$end])->chunk(100, function ($users) { $table_name = $this->option('table_name'); foreach ($users as $user) { $kuaizhao = $this->kuaizhao($user->url); $update = DB::table($table_name) ->where('id', $user->id) ->update(['snapshot' => $kuaizhao]); var_dump($user->id); } }); DB::commit(); }catch (\Exception $e){ var_dump('error in update ,rollback~ '); DB::rollBack(); } } /** * 将回流url转换成快照 */ public function kuaizhao($url) { $text = $url; $url = 'http://www.baidu.com/s?word='.$text; $curl = app('curl'); $html = $curl->get($url); $regex = "/<a.?data-click=\"{'rsv_snapshot':'1'}.*?\">(.*?)<\/a>/is"; preg_match($regex,$html,$matches); $regex1 = "/(href=\").*?(\")/i"; if (!isset($matches[0])){ return $text; }else{ preg_match($regex1,$matches[0],$matches1); $replaceList = array("href=\"","\""); $url = str_replace($replaceList, "", $matches1[0]); return $url; } } }
使用方法:php artisan huiliu:snapshot --table_name=XXXXX --start=12
功能:将会执行上面的handel方法,修改表名为XXXX的表中从id为12行开始到结束的url为对应的百度快照。
注:XXX根据需求修改
创建自己的 laravel Artisan Console
http://blog.csdn.net/qq_28018283/article/details/53337166
相关文章推荐
- Laravel 创建可以传递参数 Console 服务
- laravel框架-php artisan db:seed --class=RoleSeeder 报错 Class RoleSeeder does not exist
- [李景山php]每天laravel[023]-laravel 中级任务--小白教程----实际操作-创建基础任务 to-do-list 案例 ---登录,该死的登录
- laravel php artisan 错误
- [李景山php]每天laravel[023]-laravel 中级任务--小白教程----实际操作-创建基础任务 to-do-list 案例 ---删除数据
- 在laravel中执行数据库迁移报在执行数据库迁移时提示:php artisan migrate SQLSTATE[42000]: Syntax error or access violation
- laravel一些特别爽的指令php artisan
- PHP laravel系列之PHP Artisan Tinker
- laravel5.1下php artisan migrate使用注意事项
- Laravel中没有php artisan make:view怎么办?(可以基于Artisan View扩展包创建视图文件的方法解决!)
- 使用 Php Artisan Tinker 来调试你的 Laravel
- [李景山php]每天laravel[019]-laravel 中级任务--小白教程----实际操作-创建基础任务 to-do-list 案例 ---路由
- [李景山php]每天laravel[021]-laravel 中级任务--小白教程----实际操作-创建基础任务 to-do-list 案例 ---Task 任务处理控制器
- laravel 5.1下php artisan migrate的使用注意事项总结
- [李景山php]每天laravel[020]-laravel 中级任务--小白教程----实际操作-创建基础任务 to-do-list 案例 ---视图
- Laravel 5.1使用命令行模式(artisan)运行php脚本
- [李景山php]每天laravel[022]-laravel 中级任务--小白教程----实际操作-创建基础任务 to-do-list 案例 ---Task 任务处理控制器--显示主页
- laravel框架学习(三)win下php artisan tinker 测试数据时,无法运行报错的解决方法
- 创建自己的 laravel Artisan Console
- phpstom 实用laravel 需要附加的 命令