本文实例为大家分享了微信开发php代码,供大家参考,具体内容如下
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
|
<?php //封装成一个微信接口类 class WeixinApi { private $appid ; private $appsecret ; //构造方法 初始化赋值 public function __construct( $appid = "" , $appsecret = "" ) { $this ->appid = $appid ; $this ->appsecret = $appsecret ; } //验证服务器地址有效性 public function valid() { if ( $this ->checkSignature()) { $echostr = $_GET [ 'echostr' ]; //随机的字符串 return $echostr ; } else { return "Error" ; } } //检查签名 private function checkSignature() { //一、接收微信服务器GET方式提交过来的4个参数数据 $signature = $_GET [ 'signature' ]; //微信加密签名 $timestamp = $_GET [ 'timestamp' ]; //时间戳 $nonce = $_GET [ 'nonce' ]; //随机数 //二、加密/校验过程 // 1. 将token、timestamp、nonce三个参数进行字典序排序; // bool sort ( array &$array [, int $sort_flags = SORT_REGULAR ] ) 对数组排序 $tmpArr = array (TOKEN, $timestamp , $nonce ); //将上面三个参数放到一个数组里面 sort( $tmpArr ,SORT_STRING); // 2. 将三个参数字符串拼接成一个字符串进行sha1加密; $tmpStr = implode( $tmpArr ); //将数组转化成字符串 $signatureStr = sha1( $tmpStr ); // 3. 开发者获得加密后的字符串与signature对比。 if ( $signatureStr == $signature ) { return true; } else { return false; } } //响应消息 public function responseMsg() { //接收微信服务器发送POST请求到开发者服务器,携带的XML数据包 $postData = $GLOBALS [ 'HTTP_RAW_POST_DATA' ]; //处理xml数据包 $xmlObj = simplexml_load_string( $postData , "SimpleXMLElement" ,LIBXML_NOCDATA); if (! $xmlObj ) { echo "" ; exit ; } //获取接收消息中的参数内容 $toUserName = $xmlObj ->ToUserName; //开发者微信号 $fromUserName = $xmlObj ->FromUserName; //发送方的微信号(openid) $msgType = $xmlObj ->MsgType; //消息类型 switch ( $msgType ) { //接收文本消息 case 'text' : //获取文本消息的关键字 $keyword = $this ->receiveText( $xmlObj ); //进行关键字回复 switch ( $keyword ) { case "w001" : case "W001" : return $this ->replyText( $xmlObj , "Hi~你好" ); break ; case "w002" : case "W002" : return $this ->replyText( $xmlObj , "Hi~尴尬了" ); break ; case "笑话" : $key = "dee9ebc68fd5a61f67286063932afe56" ; return $this ->replyNews( $xmlObj , $this ->joke_text( $key )); break ; default : $key = "dee9ebc68fd5a61f67286063932afe56" ; return $this ->replyNews( $xmlObj , $this ->joke_text( $key )); break ; } break ; //接收图片消息 case 'image' : return $this ->receiveImage( $xmlObj ); break ; //接收事件推送 case 'event' : return $this ->receiveEvent( $xmlObj ); break ; } } //接收事件推送 public function receiveEvent( $obj ) { //接收事件类型 $event = $obj ->Event; switch ( $event ) { //关注事件 case 'subscribe' : //下发欢迎消息 $newsArr = array ( array ( "Title" => "做有价值的头条资讯!" , "Description" => "把握价值头条资讯,日常更加有谈资呢!" , "PicUrl" => "http://jober.applinzi.com/news/img/news.png" , "Url" => "http://jober.applinzi.com/news/index.php" ) ); //回复图文消息 return $this ->replyNews( $obj , $newsArr ); break ; //取消关注事件 case 'unsubscribe' : //账号的解绑操作等等 break ; //自定义菜单推送CLICK事件 case 'CLICK' : $eventKey = $obj ->EventKey; //获取事件KEY值,与自定义菜单接口中KEY值对应 switch ( $eventKey ) { case 'old' : $weixinArr = $this ->history( "da675ebc6a0d72920dca3f676122a693" ); $weixinArr = array_slice ( $weixinArr , 0,5); $newsArr = array (); foreach ( $weixinArr as $item ) { $newsArr = array ( array ( "Title" => $item [ 'Description' ], "Description" => $item [ 'Title' ], "PicUrl" => "http://1.jober.applinzi.com/news/img/2.jpg" , "Url" => "http://www.todayonhistory.com/" )); } return $this ->replyNews( $obj , $newsArr ); break ; } break ; } } //接收文本消息 public function receiveText( $obj ) { $content = trim( $obj ->Content); //文本消息的内容 return $content ; } //接收图片消息 public function receiveImage( $obj ) { $picUrl = $obj ->PicUrl; //图片的链接 $mediaId = $obj ->MediaId; //图片消息媒体id return $this ->replyImage( $obj , $mediaId ); } //回复图片消息 public function replyImage( $obj , $mediaId ) { $replyXml = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[image]]></MsgType> <Image> <MediaId><![CDATA[%s]]></MediaId> </Image> </xml>"; return sprintf( $replyXml , $obj ->FromUserName, $obj ->ToUserName,time(), $mediaId ); } //回复文本消息 public function replyText( $obj , $content ) { $replyXml = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[%s]]></Content> </xml>"; return sprintf( $replyXml , $obj ->FromUserName, $obj ->ToUserName,time(), $content ); } //回复图文消息 public function replyNews( $obj , $newsArr ) { //判断是否为数组类型 if (! is_array ( $newsArr )) { return ; } // 判断数组是否为空数组 if (! $newsArr ) { return ; } $itemStr = "" ; //定义item模板 $itemXml = "<item> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <PicUrl><![CDATA[%s]]></PicUrl> <Url><![CDATA[%s]]></Url> </item>"; foreach ( $newsArr as $item ) { $itemStr .= sprintf( $itemXml , $item [ 'Title' ], $item [ 'Description' ], $item [ 'PicUrl' ], $item [ 'Url' ]); } $replyXml = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[news]]></MsgType> <ArticleCount> ".count($newsArr)." </ArticleCount> <Articles> ".$itemStr." </Articles> </xml>"; return sprintf( $replyXml , $obj ->FromUserName, $obj ->ToUserName,time()); } //封装https请求(GET和POST) protected function https_request( $url , $data =null) { //1、初始化curl $ch = curl_init(); //2、设置传输选项 curl_setopt( $ch , CURLOPT_URL, $url ); //请求的url地址 curl_setopt( $ch ,CURLOPT_RETURNTRANSFER,1); //将请求的结果以文件流的形式返回 if (! empty ( $data )) { curl_setopt( $ch ,CURLOPT_POST,1); //请求POST方式 curl_setopt( $ch ,CURLOPT_POSTFIELDS, $data ); //POST提交的内容 } //3、执行请求并处理结果 $outopt = curl_exec( $ch ); //把json数据转化成数组 $outoptArr = json_decode( $outopt ,TRUE); //4、关闭curl curl_close( $ch ); //如果返回的结果$outopt是json数据,则需要判断一下 if ( is_array ( $outoptArr )) { return $outoptArr ; } else { return $outopt ; } } public function juhe_weixin( $key , $type ) { $url = "http://v.juhe.cn/toutiao/index?type={$type}&key={$key}" ; $result = $this ->https_request( $url ); if ( $result [ 'error_code' ] == 0) { return $result [ 'result' ][ 'data' ]; } else { return array (); } } //聚合数据-获取最新趣图 public function joke_text( $key , $pagesize =10) { $url = "http://japi.juhe.cn/joke/img/text.from?key={$key}&pagesize={$pagesize}" ; $jokeArr = $this ->https_request( $url ); $resultArr = $jokeArr [ 'result' ][ 'data' ]; // $content = $resultArr[0]['content']; // return $this->replyText($xmlObj,$content); $newsArr = array (); //判断笑话接口是否获取数据 if ( $jokeArr [ 'error_code' ] == 0) { foreach ( $resultArr as $item ) { $newsArr [] = array ( "Title" => $item [ 'content' ], "Description" => $item [ 'updatetime' ], "PicUrl" => $item [ 'url' ], "Url" => $item [ 'url' ] ); } } return $newsArr ; } //聚合数据-获取历史上的今天 public function history( $key ) { $m = idate( 'm' ); $d = idate( 'd' ); $day = "{$m}/{$d}" ; $url = "http://v.juhe.cn/todayOnhistory/queryEvent.php?key={$key}&date={$day}" ; $historyArr = $this ->https_request( $url ); $resultArr = $historyArr [ 'result' ]; // $content = $resultArr['title']; // return $this->replyText($xmlObj,$content); $newsArr = array (); //判断接口是否获取数据 if ( $jokeArr [ 'error_code' ] == 0) { foreach ( $resultArr as $item ) { $newsArr [] = array ( "Title" => $item [ 'title' ], "Description" => $item [ 'date' ], "PicUrl" => "" , "Url" => "" ); } } return $newsArr ; } public function fund( $key ) { $url = "http://japi.juhe.cn/jingzhi/query.from?key={$key}" ; $fundArr = $this ->https_request( $url ); $resultArr = $fundArr [ 'result' ]; // $content = $resultArr['title']; // return $this->replyText($xmlObj,$content); $newsArr = array (); //判断接口是否获取数据 if ( $jokeArr [ 'error_code' ] == 0) { foreach ( $resultArr as $item ) { $newsArr [] = array ( "Title" => $item [ 'day' ], "Description" => $item [ 'title' ], "PicUrl" => "" , "Url" => "http://www.baidu.com" ); } } return $newsArr ; } /** *获取基础支持里面的接口调用凭证access_token并缓存access_token *@return access_token string 接口凭证 **/ public function getAccessToken() { //获取memcache缓存的access_token $access_token = $this ->_memcache_get( "access_token" ); //如果缓存的access_token失效 if (! $access_token ) { //如果失效调用获取接口凭证来获取access_token $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appid}&secret={$this->appsecret}" ; $outoptArr = $this ->https_request( $url ); if (!isset( $outoptArr [ 'errcode' ])) { //memcache缓存access_token $this ->_memcache_set( "access_token" , $outoptArr [ 'access_token' ],7000); return $outoptArr [ 'access_token' ]; } } return $access_token ; } //初始化memcache private function _memcache_init() { $mmc = new Memcache; $ret = $mmc -> connect(); if ( $ret == false) { return ; } return $mmc ; } //设置memcache private function _memcache_set( $key , $value , $time =0) { $mmc = $this ->_memcache_init(); $mmc -> set( $key , $value ,0, $time ); } //获取memcahce private function _memcache_get( $key ) { $mmc = $this ->_memcache_init(); return $mmc -> get( $key ); } //自定义菜单创建 public function menu_create( $data ) { $access_token = $this ->getAccessToken(); //自定义菜单创建接口地址 $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token={$access_token}" ; return $this ->https_request( $url , $data ); } //自定义菜单删除 public function menu_delete() { $access_token = $this ->getAccessToken(); $url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token={$access_token}" ; return $this ->https_request( $url ); } } ?> |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。