微信平台配置服务器,提供三种加密解密模式供开发者选择,明文,兼容,安全。选择兼容和安全模式之前,需要在开发者中心填写EncodingAESKey.公众号用这个秘钥对收到的密文进行解密,回复也用这个加密。
接口程序需要配置如下三个参数
define("TOKEN","weixin");
define("AppID","写自己的AppID");
define("EncodingAESKey","填写自己的信息")
require_once('wxBizMsgCrypt,php');
用户向公众号发送消息,微信公众号会在URL中带上签名,时间戳,随机数,encrypt_type,msg_signature等参数。
同时向接口发送XML数据包
程序需要如下几个参数,用于加密解密过程
$timestamp=$_GET["timestamp"];
$nonce=$_GET[""nonce];
$msg_signature=$_GET['msg_signature'];
$encrypt_type=$_GET['encrypt_type'];
接口程序收到消息,先进行解密
$postStr=$GLOBALS["HTTP_RAW_POST_DATA"];//接受post
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>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType>![CDATA[%s]]</MsgType>
<Content><![CDATA[%s]]></Content>
</xml>
把上边信息进行加密后返回给微信公众号
if($encrypt_type=='aes'){
$encryptMsg='';
$errCode=$pc->encryptMsg($result,$timeStamp,$nonce,$encryptMsg);
$result=$encryptMsg;
$this->logger("E \r\n".$result);
}