微信公众平台开发者中心安全模式消息体加解密实现,公众开发者关键字:微信公众平台 消息体签名 消息体加解密 encodingaeskey 安全模式
原文 http://www.cnblogs.com/txw1958/p/weixin-aes-encrypt-decrypt.html
一、消息体加解密微信公众平台在配置服务器时,提供了3种加解密的模式供开发者选择,即明文模式、兼容模式、安全模式,选择兼容模式和安全模式前,需在开发者中心填写消息加解密密钥encodingaeskey。
明文模式:维持现有模式,没有适配加解密新特性,消息体明文收发,默认设置为明文模式兼容模式:公众平台发送消息内容将同时包括明文和密文,消息包长度增加到原来的3倍左右;公众号回复明文或密文均可,不影响现有消息收发;开发者可在此模式下进行调试安全模式(推荐):公众平台发送消息体的内容只含有密文,公众账号回复的消息体也为密文,建议开发者在调试成功后使用此模式收发消息什么是encodingaeskey?
微信公众平台采用aes对称加密算法对推送给公众帐号的消息体对行加密,encodingaeskey则是加密所用的秘钥。公众帐号用此秘钥对收到的密文消息体进行解密,回复消息体也用此秘钥加密。aes对称加密算法的原理可以参考 http://www.cnblogs.com/txw1958/p/aes.html加解密的详细技术方案可以参考官方文档 http://mp.weixin.qq.com/wiki/index.php?title=%e6%8a%80%e6%9c%af%e6%96%b9%e6%a1%88
适用公众账号类型
已认证订阅号服务号 不能用于未认证订阅号,因为其没有appid参数
二、开发实现及数据分析1. 配置假设本次的开发配置中url为
http://www.fangbei.org/index.php
接口程序中需要配置以下三项参数
/* 方倍工作室 http://www.cnblogs.com/txw1958/ copyright 2014 all rights reserved*/define(token, weixin);define(appid, wxbad0b45542aa0b5e);define(encodingaeskey, abcdefghijklmnopqrstuvwxyz0123456789abcdefg);require_once('wxbizmsgcrypt.php');
2. 加解密实现当用户向公众账号发送消息时,微信公众账号将会在url中带上signature、timestamp、nonce、encrypt_type、msg_signature等参数,如下所示
http://www.fangbei.org/index.php?signature=35703636de2f9df2a77a662b68e521ce17c34db4×tamp=1414243737&nonce=1792106704&encrypt_type=aes&msg_signature=6147984331daf7a1a9eed6e0ec3ba69055256154
同时向该接口推送如下xml消息 ,即一个已加密的消息
这时,程序需要从url中获得以下参数
$timestamp = $_get['timestamp'];$nonce = $_get[nonce];$msg_signature = $_get['msg_signature'];$encrypt_type = $_get['encrypt_type'];
这些参数将用于加解密过程
收到消息后,先进行解密,解密部分代码如下
$poststr = $globals[http_raw_post_data];if ($encrypt_type == 'aes'){ $pc = new wxbizmsgcrypt(token, encodingaeskey, appid); $this->logger( d \r\n.$poststr); $decryptmsg = ; //解密后的明文 $errcode = $pc->decryptmsg($msg_signature, $timestamp, $nonce, $poststr, $decryptmsg); $poststr = $decryptmsg;}
解密完成后,把解密内容又返回给$poststr,这是为了保证将消息中解密后的内容和明文模式时的消息统一,方便后续处理,解密后的xml如下
1414243737 6074130599188426998
对消息在自己的原来代码中处理,完成之后,要回复的消息如下
1414243733
把上述消息进行加密,返回给微信公众账号
//加密if ($encrypt_type == 'aes'){ $encryptmsg = ''; //加密后的密文 $errcode = $pc->encryptmsg($result, $timestamp, $nonce, $encryptmsg); $result = $encryptmsg; $this->logger( e \r\n.$result);}
加密后的内容如下
1414243733
这样,一个安全模式下的加解密消息就完成了。
三、完整代码 1 responsemsg(); 14 }else{ 15 $wechatobj->valid(); 16 } 17 18 class wechatcallbackapitest 19 { 20 //验证签名 21 public function valid() 22 { 23 $echostr = $_get[echostr]; 24 $signature = $_get[signature]; 25 $timestamp = $_get[timestamp]; 26 $nonce = $_get[nonce]; 27 $tmparr = array(token, $timestamp, $nonce); 28 sort($tmparr); 29 $tmpstr = implode($tmparr); 30 $tmpstr = sha1($tmpstr); 31 if($tmpstr == $signature){ 32 echo $echostr; 33 exit; 34 } 35 } 36 37 //响应消息 38 public function responsemsg() 39 { 40 $timestamp = $_get['timestamp']; 41 $nonce = $_get[nonce]; 42 $msg_signature = $_get['msg_signature']; 43 $encrypt_type = (isset($_get['encrypt_type']) && ($_get['encrypt_type'] == 'aes')) ? aes : raw; 44 45 $poststr = $globals[http_raw_post_data]; 46 if (!empty($poststr)){ 47 //解密 48 if ($encrypt_type == 'aes'){ 49 $pc = new wxbizmsgcrypt(token, encodingaeskey, appid); 50 $this->logger( d \r\n.$poststr); 51 $decryptmsg = ; //解密后的明文 52 $errcode = $pc->decryptmsg($msg_signature, $timestamp, $nonce, $poststr, $decryptmsg); 53 $poststr = $decryptmsg; 54 } 55 $this->logger( r \r\n.$poststr); 56 $postobj = simplexml_load_string($poststr, 'simplexmlelement', libxml_nocdata); 57 $rx_type = trim($postobj->msgtype); 58 59 //消息类型分离 60 switch ($rx_type) 61 { 62 case event: 63 $result = $this->receiveevent($postobj); 64 break; 65 case text: 66 $result = $this->receivetext($postobj); 67 break; 68 } 69 $this->logger( r \r\n.$result); 70 //加密 71 if ($encrypt_type == 'aes'){ 72 $encryptmsg = ''; //加密后的密文 73 $errcode = $pc->encryptmsg($result, $timestamp, $nonce, $encryptmsg); 74 $result = $encryptmsg; 75 $this->logger( e \r\n.$result); 76 } 77 echo $result; 78 }else { 79 echo ; 80 exit; 81 } 82 } 83 84 //接收事件消息 85 private function receiveevent($object) 86 { 87 $content = ; 88 switch ($object->event) 89 { 90 case subscribe: 91 $content = 欢迎关注方倍工作室 ; 92 break; 93 } 94 95 $result = $this->transmittext($object, $content); 96 return $result; 97 } 98 99 //接收文本消息100 private function receivetext($object)101 {102 $keyword = trim($object->content);103 if (strstr($keyword, 文本)){104 $content = 这是个文本消息;105 }else if (strstr($keyword, 单图文)){106 $content = array();107 $content[] = array(title=>单图文标题, description=>单图文内容, picurl=>http://discuz.comli.com/weixin/weather/icon/cartoon.jpg, url =>http://m.cnblogs.com/?u=txw1958);108 }else if (strstr($keyword, 图文) || strstr($keyword, 多图文)){109 $content = array();110 $content[] = array(title=>多图文1标题, description=>, picurl=>http://discuz.comli.com/weixin/weather/icon/cartoon.jpg, url =>http://m.cnblogs.com/?u=txw1958);111 $content[] = array(title=>多图文2标题, description=>, picurl=>http://d.hiphotos.bdimg.com/wisegame/pic/item/f3529822720e0cf3ac9f1ada0846f21fbe09aaa3.jpg, url =>http://m.cnblogs.com/?u=txw1958);112 $content[] = array(title=>多图文3标题, description=>, picurl=>http://g.hiphotos.bdimg.com/wisegame/pic/item/18cb0a46f21fbe090d338acc6a600c338644adfd.jpg, url =>http://m.cnblogs.com/?u=txw1958);113 }else if (strstr($keyword, 音乐)){114 $content = array();115 $content = array(title=>最炫民族风, description=>歌手:凤凰传奇, musicurl=>http://121.199.4.61/music/zxmzf.mp3, hqmusicurl=>http://121.199.4.61/music/zxmzf.mp3);116 }else{117 $content = date(y-m-d h:i:s,time()).\n.$object->fromusername.\n技术支持 方倍工作室;118 }119 120 if(is_array($content)){121 if (isset($content[0])){122 $result = $this->transmitnews($object, $content);123 }else if (isset($content['musicurl'])){124 $result = $this->transmitmusic($object, $content);125 }126 }else{127 $result = $this->transmittext($object, $content);128 }129 return $result;130 }131 132 //回复文本消息133 private function transmittext($object, $content)134 {135 $xmltpl = 136 137 138 %s139 140 141 ;142 $result = sprintf($xmltpl, $object->fromusername, $object->tousername, time(), $content);143 return $result;144 }145 146 //回复图文消息147 private function transmitnews($object, $newsarray)148 {149 if(!is_array($newsarray)){150 return;151 }152 $itemtpl = 153 154 155 156 157 158 ;159 $item_str = ;160 foreach ($newsarray as $item){161 $item_str .= sprintf($itemtpl, $item['title'], $item['description'], $item['picurl'], $item['url']);162 }163 $xmltpl = 164 165 166 %s167 168 %s169 170 $item_str 171 ;172 173 $result = sprintf($xmltpl, $object->fromusername, $object->tousername, time(), count($newsarray));174 return $result;175 }176 177 //回复音乐消息178 private function transmitmusic($object, $musicarray)179 {180 $itemtpl = 181 182 183 184 185 ;186 187 $item_str = sprintf($itemtpl, $musicarray['title'], $musicarray['description'], $musicarray['musicurl'], $musicarray['hqmusicurl']);188 189 $xmltpl = 190 191 192 %s193 194 $item_str195 ;196 197 $result = sprintf($xmltpl, $object->fromusername, $object->tousername, time());198 return $result;199 }200 201 //日志记录202 public function logger($log_content)203 {204 if(isset($_server['http_appname'])){ //sae205 sae_set_display_errors(false);206 sae_debug($log_content);207 sae_set_display_errors(true);208 }else if($_server['remote_addr'] != 127.0.0.1){ //local209 $max_size = 500000;210 $log_filename = log.xml;211 if(file_exists($log_filename) and (abs(filesize($log_filename)) > $max_size)){unlink($log_filename);}212 file_put_contents($log_filename, date('y-m-d h:i:s').$log_content.\r\n, file_append);213 }214 }215 }216 ?>
微信公众平台的高级功可以点进去页面显示不存在?
你是新注册的账号吗?要是新账号,不显示应该是因为还没被审核通过吧。要不是新账号,就清理下浏览器缓存,要么就换台机器试试。应该不会有别的原因了。
微信公众平台开发者模式回复多条图文消息代码
可以将单个图文消息抽象出来作为一个循环, 把每篇文章的内容填写进去好了后, 再拼接起来, 这样是最快的. 我也是做微信开发模式第三方开发的, 可以看我的资料, 个人简介.
http://www.bkjia.com/phpjc/907044.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/907044.htmltecharticle微信公众平台开发者中心安全模式消息体加解密实现,公众开发者 关键字:微信公众平台 消息体签名 消息体加解密 encodingaeskey 安全模式...