前言
在Yii中實(shí)現(xiàn)場(chǎng)景二維碼這里我使用的是easywechat插件,安裝easywechat插件
1
|
composer require jianyan74/yii2-easy-wechat |
github地址: https://github.com/jianyan74/yii2-easy-wechat
easywechat文檔地址: https://www.easywechat.com/docs/master/overview
生成場(chǎng)景二維碼前提:
微信的場(chǎng)景二維碼功能主要是生成一個(gè)微信二維碼,然后在手機(jī)使用微信掃描此二維碼時(shí),會(huì)觸發(fā)微信通知,所以我們?cè)谏蓤?chǎng)景二維碼之前進(jìn)行微信的服務(wù)端驗(yàn)證
1:服務(wù)端驗(yàn)證
1
2
3
4
5
|
$app = Yii:: $app ->wechat->getApp(); $server = $app ->server; $response = $server ->serve(); $response ->send(); exit (); |
服務(wù)端驗(yàn)證代碼完成之后在微信公眾號(hào)進(jìn)行服務(wù)端驗(yàn)證即可
2:生成場(chǎng)景二維碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
$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 //獲取參數(shù) if ( $message [ 'Event' ] == 'SCAN' ) { $code = $message [ 'EventKey' ]; } else { $code = str_replace ( 'qrscene_' , '' , $message [ 'EventKey' ]); } //發(fā)送圖文消息 $items = [ new NewsItem([ 'title' => '圖文標(biāo)題' , 'description' => '圖文描述' , 'url' => '圖文鏈接' , 'image' => '圖文圖片, ]), ]; return new News( $items ); } break ; default : break ; } }); $server = $app ->server; $response = $server ->serve(); $response ->send(); exit (); |
根據(jù)如上就可以實(shí)現(xiàn)場(chǎng)景二維碼
總結(jié)
到此這篇關(guān)于Yii實(shí)現(xiàn)微信公眾號(hào)場(chǎng)景二維碼的文章就介紹到這了,更多相關(guān)Yii實(shí)現(xiàn)微信公眾號(hào)場(chǎng)景二維碼內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://segmentfault.com/a/1190000023806676