前言
在Yii中實現場景二維碼這里我使用的是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
生成場景二維碼前提:
微信的場景二維碼功能主要是生成一個微信二維碼,然后在手機使用微信掃描此二維碼時,會觸發微信通知,所以我們在生成場景二維碼之前進行微信的服務端驗證
1:服務端驗證
1
2
3
4
5
|
$app = Yii:: $app ->wechat->getApp(); $server = $app ->server; $response = $server ->serve(); $response ->send(); exit (); |
服務端驗證代碼完成之后在微信公眾號進行服務端驗證即可
2:生成場景二維碼
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 //獲取參數 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 (); |
根據如上就可以實現場景二維碼
總結
到此這篇關于Yii實現微信公眾號場景二維碼的文章就介紹到這了,更多相關Yii實現微信公眾號場景二維碼內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://segmentfault.com/a/1190000023806676