本文實(shí)例講述了Yii Framework框架開(kāi)發(fā)微信公眾平臺(tái)。分享給大家供大家參考,具體如下:
1. 先到微信公眾平臺(tái)注冊(cè)帳號(hào)
http://mp.weixin.qq.com
2. 下載demo
微信公眾平臺(tái)提供了一個(gè)十分“樸素”的demo,說(shuō)明如何調(diào)用消息接口的。代碼真的很樸素,具體內(nèi)容可到官網(wǎng)下載。
3. 按照Yii的規(guī)則,做一個(gè)extension。
這里命名為 weixin,目錄結(jié)構(gòu)如下:
? extensions/
? weixin/
Weixin.php*
Weixin.php代碼內(nèi)容:
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
<?php /** * WeixinCallback * * @package * @version $id$ * @copyright 1997-2005 The PHP Group * @author davidhhuan@126.com * {@link <a href="http://www.sharefamily.net" rel="external nofollow" target="_blank">http://www.sharefamily.net</a>} */ class Weixin { //$_GET參數(shù) public $signature ; public $timestamp ; public $nonce ; public $echostr ; // public $token ; public $debug = false; public $msg = array (); public $setFlag = false; /** * __construct * * @param mixed $params * @access public * @return void */ public function __construct( $params ) { foreach ( $params as $k1 => $v1 ) { if (property_exists( $this , $k1 )) { $this -> $k1 = $v1 ; } } } /** * valid * * @access public * @return void */ public function valid() { //valid signature , option if ( $this ->checkSignature()){ echo $this ->echostr; Yii::app()-> end (); } } /** * 獲得用戶發(fā)過(guò)來(lái)的消息(消息內(nèi)容和消息類型 ) * * @access public * @return void */ public function init() { $postStr = empty ( $GLOBALS [ "HTTP_RAW_POST_DATA" ]) ? '' : $GLOBALS [ "HTTP_RAW_POST_DATA" ]; if ( $this ->debug) { $this ->log( $postStr ); } if (! empty ( $postStr )) { $this ->msg = simplexml_load_string( $postStr , 'SimpleXMLElement' , LIBXML_NOCDATA); } } /** * makeEvent * * @access public * @return void */ public function makeEvent() { } /** * 回復(fù)文本消息 * * @param string $text * @access public * @return void */ public function makeText( $text = '' ) { $createTime = time(); $funcFlag = $this ->setFlag ? 1 : 0; $textTpl = "<xml> <ToUserName><![CDATA[{ $this ->msg->FromUserName}]]></ToUserName> <FromUserName><![CDATA[{ $this ->msg->ToUserName}]]></FromUserName> <CreateTime>{ $createTime }</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>%s</FuncFlag> </xml>"; return sprintf( $textTpl , $text , $funcFlag ); } /** * 根據(jù)數(shù)組參數(shù)回復(fù)圖文消息 * * @param array $newsData * @access public * @return void */ public function makeNews( $newsData = array ()) { $createTime = time(); $funcFlag = $this ->setFlag ? 1 : 0; $newTplHeader = "<xml> <ToUserName><![CDATA[{ $this ->msg->FromUserName}]]></ToUserName> <FromUserName><![CDATA[{ $this ->msg->ToUserName}]]></FromUserName> <CreateTime>{ $createTime }</CreateTime> <MsgType><![CDATA[news]]></MsgType> <ArticleCount>%s</ArticleCount><Articles>"; $newTplItem = "<item> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <PicUrl><![CDATA[%s]]></PicUrl> <Url><![CDATA[%s]]></Url> </item>"; $newTplFoot = "</Articles> <FuncFlag>%s</FuncFlag> </xml>"; $content = '' ; $itemsCount = count ( $newsData [ 'items' ]); //微信公眾平臺(tái)圖文回復(fù)的消息一次最多10條 $itemsCount = $itemsCount < 10 ? $itemsCount : 10; if ( $itemsCount ) { foreach ( $newsData [ 'items' ] as $key => $item ) { if ( $key <=9) { $content .= sprintf( $newTplItem , $item [ 'title' ], $item [ 'description' ], $item [ 'picurl' ], $item [ 'url' ]); } } } $header = sprintf( $newTplHeader , $itemsCount ); $footer = sprintf( $newTplFoot , $funcFlag ); return $header . $content . $footer ; } /** * reply * * @param mixed $data * @access public * @return void */ public function reply( $data ) { if ( $this ->debug) { $this ->log( $data ); } echo $data ; } /** * checkSignature * * @access private * @return void */ private function checkSignature() { $tmpArr = array ( $this ->token, $this ->timestamp, $this ->nonce); sort( $tmpArr ); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if ( $tmpStr == $this ->signature ){ return true; } else { return false; } } /** * log * * @access private * @return void */ private function log( $log ) { if ( $this ->debug) { file_put_contents (Yii::getPathOfAlias( 'application' ). '/runtime/weixin_log.txt' , var_export( $log , true). "\n\r" , FILE_APPEND); } } } |
使用方法,這里舉例在SiteController里面
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
34
35
36
37
38
39
40
41
42
43
44
|
/** * actionIndex * * @access public * @return void */ public function actionIndex() { $weixin = new Weixin( $_GET ); $weixin ->token = $this ->_weixinToken; //$weixin->debug = true; //網(wǎng)址接入時(shí)使用 if (isset( $_GET [ 'echostr' ])) { $weixin ->valid(); } $weixin ->init(); $reply = '' ; $msgType = empty ( $weixin ->msg->MsgType) ? '' : strtolower ( $weixin ->msg->MsgType); switch ( $msgType ) { case 'text' : //你要處理文本消息代碼 break ; case 'image' : //你要處理圖文消息代碼 break ; case 'location' : //你要處理位置消息代碼 break ; case 'link' : //你要處理鏈接消息代碼 break ; case 'event' : //你要處理事件消息代碼 break ; default : //無(wú)效消息情況下的處理方式 break ; } $weixin ->reply( $reply ); } |
至此,基本的邏輯都實(shí)現(xiàn)了
希望本文所述對(duì)大家基于Yii框架的PHP程序設(shè)計(jì)有所幫助。
原文鏈接:https://www.cnblogs.com/davidhhuan/archive/2013/04/02/2996526.html