在 Yii 中实现场景二维码这里我应用的是 easywechat 插件,装置 easywechat 插件
composer require jianyan74/yii2-easy-wechat
github 地址:https://github.com/jianyan74/yii2-easy-wechat
easywechat 文档地址:https://www.easywechat.com/docs/master/overview
生成场景二维码前提:
微信的场景二维码性能次要是生成一个微信二维码,而后在手机应用微信扫描此二维码时,会触发微信告诉,所以咱们在生成场景二维码之前进行微信的服务端验证
1:服务端验证
$app = Yii::$app->wechat->getApp();
$server = $app->server;
$response = $server->serve();
$response->send();
exit();
服务端验证代码实现之后在微信公众号进行服务端验证即可
2:生成场景二维码
$app = Yii::$app->wechat->getApp();
$app->server->push(function ($message) use ($app) {switch ($message['MsgType']){
case 'event':
// 扫码事件:SCAN 订阅事件:subscribe
if (isset($message['Event']) && ($message['Event'] == 'SCAN' || $message['Event'] == 'subscribe')) {$openId = $message['FromUserName'];// 扫面用户的 openID
// 获取参数
if ($message['Event'] == 'SCAN') {$code= $message['EventKey'];
} else {$code= str_replace('qrscene_', '', $message['EventKey']);
}
// 发送图文音讯
$items = [
new NewsItem([
'title'=> '图文题目',
'description' => '图文形容',
'url' => '图文链接',
'image'=> ' 图文图片,
]),
];
return new News($items);
}
break;
default:
break;
}
});
$server = $app->server;
$response = $server->serve();
$response->send();
exit();
依据如上就能够实现场景二维码