laravel php artisan console 二. 附加参数与案例

16 篇文章 0 订阅
<?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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值