php 同步微信大量粉丝在数据表,使用 EasyWechat 同步微信粉丝到数据库
这篇文章方法写的有些纠结,也有些问题,权当交流用,我测试同步180个粉丝的数据,完成队列任务的时间是53秒。如果社区的大牛们有更好的方法,请赐教。如果对接多公众号还有疑问,这里有传送门:需要同步的控制器,例如:WechatManageController.php写入方法://WechatConfigHandlers 写法查看上面的链接public function fans_sync(Wechat
这篇文章方法写的有些纠结,也有些问题,权当交流用,我测试同步180个粉丝的数据,完成队列任务的时间是53秒。如果社区的大牛们有更好的方法,请赐教。
如果对接多公众号还有疑问,这里有传送门:
需要同步的控制器,例如:
WechatManageController.php
写入方法:
//WechatConfigHandlers 写法查看上面的链接
public function fans_sync(WechatConfigHandlers $wechat, $account)
{
$app = $this->wechat->app($account);
$fans = $app->user->list()['data']['openid'];
$job = $this->dispatch(new WechatFansSync($fans));
if ($job) {
return redirect()->to('/wechat/fans/list/')->with('success', '同步成功');
}
}
创建一个队列任务:
php artisan make:job WechatFansSync
然后在队列任务文件里加入以下代码:
protected $fans;
public function __construct($fans)
{
$this->fans = $fans;
}
public function handle(WechatConfigHandlers $wechat, $account)
{
$app = $wechat->app($account);
foreach ($this->fans as $fan) {
$wefan = WechatFans::where('openid',$fan)->first();
if (! $wefan) {
$user = $app->user->get($fan);
$create_data = WechatFans::create([
'aid' => $account,
'openid' => $user['openid'],
'name' => $user['nickname'],
'nickname' => $user['nickname'],
'avatar' => $user['headimgurl'],
'sex' => $user['sex'],
'subscribe_time' => $user['subscribe_time'],
'city' => $user['city'],
'province' => $user['province'],
'country' => $user['country'],
'remark' => $user['remark'],
'tagid_list' => $user['tagid_list'],
'subscribe_scene' => $user['subscribe_scene'],
'qr_scene' => $user['qr_scene'],
'qr_scene_str' => $user['qr_scene_str'],
'groupid' => $user['groupid'],
'subscribe' => $user['subscribe'],
]);
if ($create_data) {
\Log::info('Sync Username:'.$user['openid']);
}
}
}
}
public function failed()
{
dump('failed');
}
上面的方法写完以后,运行:
php artisan work:listen
完成队列任务,建议配合 Laravel 的Horizon使用效果更佳。别忘了写路由
Route::get('/wechat/fans_sync', 'WechatManageController@fans_sync');
本作品采用《CC 协议》,转载必须注明作者和本文链接
更多推荐
所有评论(0)