您的位置:首页 > 编程语言 > PHP开发

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  artisan php laravel
相关文章推荐