推荐 方倍工作室
参照 http://www.cnblogs.com/txw1958/p/weixin-aes-encrypt-decrypt.html
基于thinkphp3.2框架实现

第一步token验证

006bYVyvgy1fhai6wwexoj30qf0fmt96.jpg

注意:上图的url地址对应index方法
控制器代码:\Wx\Controller\IndexController.class.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
<?php
namespace Wx\Controller;
use Think\Controller;
class IndexController extends Controller {
public function index() {
if (!isset($_GET['echostr'])) {
$this->getWxCrypt();
$this->responseMsg();
}else{
$this->valid();
}
}

//验证签名
private function valid()
{
$echoStr = I("get.echostr");
$signature = I("get.signature");
$timestamp = I("get.signature");
$nonce = I("get.nonce");
$tmpArr = array(C('TOKEN'), $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode($tmpArr);
$tmpStr = sha1($tmpStr);
if($tmpStr == $signature){
echo $echoStr;
exit;
}
}
}


配置文件:
\Wx\Conf\config.php
1
2
3
4
5
6
7
<?php
return array(
//'配置项'=>'配置值'

// 加载扩展的微信配置文件 weixin.php
'LOAD_EXT_CONFIG' => 'weixin',
);


\Wx\Conf\weixin.php
1
2
3
4
5
6
7
8
9
<?php
//微信配置文件
return array(
'TOKEN' => '你的token',
'APPID' => '你的APPID',
'APPSECRET' => '你的AppSecret',
'OLDENCODINGAESKEY' => '',
'NEWENCODINGAESKEY' => '你的EncodingAESKey',
);


验证成功即可提交配置

## 第二步消息加解密
控制器代码:注意:和之前代码有区别
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
<?php
namespace Wx\Controller;
use Wx\Controller\AppBaseController;
class IndexController extends Controller {

protected $wxCrypt;

public function index() {
$echostr = I("get.echostr");
if (empty($echostr)) {
$this->getWxCrypt(C('NEWENCODINGAESKEY'));
$this->responseMsg();
}else{
$this->valid();
}
}

//验证签名
private function valid()
{
$echoStr = I("get.echostr");
$signature = I("get.signature");
$timestamp = I("get.timestamp");
$nonce = I("get.nonce");
$tmpArr = array(C('TOKEN'), $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode($tmpArr);
$tmpStr = sha1($tmpStr);
if($tmpStr == $signature){
echo $echoStr;
exit;
}
}

//响应消息
private function responseMsg()
{
$postData = $GLOBALS["HTTP_RAW_POST_DATA"];
// $postData = file_get_contents("php://input");

// 第三方收到公众号平台发送的消息
$msg = '';
$errCode = $this->wxCrypt->decryptMsg(I("get.msg_signature"), I("get.timestamp"), I("get.nonce"), $postData, $msg);
if ($errCode != 0) {
//尝试之前的encodingAESKey解密
$this->getWxCrypt(C('OLDENCODINGAESKEY'));
$errCode = $this->wxCrypt->decryptMsg(I("get.msg_signature"), I("get.timestamp"), I("get.nonce"), $postData, $msg);
if ($errCode != 0) {
//解密失败
print("解密失败: ".$errCode . "\n");
exit();
}
}

if (!empty($msg)){
$postObj = simplexml_load_string($msg, 'SimpleXMLElement', LIBXML_NOCDATA);
$MsgType = trim($postObj->MsgType);

// if (($postObj->MsgType == "event") && ($postObj->Event == "subscribe" || $postObj->Event == "unsubscribe")){
//过滤关注和取消关注事件
// }else{

// }

//消息类型分离
switch ($MsgType)
{
case "text":
$result = $this->receiveText($postObj);
break;
default:
$result = "unknown msg type: ".$MsgType;
break;
}
echo $result;
}else {
echo "";
exit;
}
}

//接收文本消息
private function receiveText($object)
{
$keyword = trim($object->Content);

$content = "这是个文本消息";

$result = $this->transmitText($object, $content);
return $result;
}


//回复文本消息
private function transmitText($object, $content)
{
if (!isset($content) || empty($content)){
return "";
}

$xmlTpl = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[%s]]></Content></xml>";
$result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time(), $content);

$encryptMsg = '';
$errCode = $this->wxCrypt->encryptMsg($result, time(), $object->nonce, $encryptMsg);
if ($errCode != 0) {
print("加密后: " . $encryptMsg . "\n");
}

return $encryptMsg;
}

//获取WxCrypt对象
private function getWxCrypt($encodingAESKey) {
import('Vendor.Weixin.WXBizMsgCrypt');
$this->wxCrypt = new \WXBizMsgCrypt(C('TOKEN'), $encodingAESKey, C('APPID'));
}
}


注意:Vendor.Weixin.WXBizMsgCrypt是微信提供的加解密demo代码,自己集成到了tp的\ThinkPHP\Library\Vendor\Weixin\下

希望这篇文章能给你带来知识和乐趣,喜欢博主的文章可以加博主好友哦

有好的文章也可以向博主投稿哦