微信团队与2014年3月4日更新了公众平台sdk,点击打开链接,对排序算法进行了修改,原有的sort($tmpArr)修改为sort($tmpArr, SORT_STRING),php中sort()函数默认为SORT_REGULAR,即原来的数据类型。微信在接口验证时推送的数据为类似:signature=01e948890eb29696536d66a9aaa2943260b9f0cc&echostr=5989551905366037038×tamp=1394299438&nonce=1394551220
post时推送的为signature=c421f0190e6912339235e391a092ae9f30c08093×tamp=1394118446&nonce=244662140
php中通过$_GET获取的数据类型为string型,即$timestamp虽然为数字但数据格式仍为string,sort排序仍然会以SORT_STRING方式进行排序。其他一些语言如java需要声明变量类型时会产生差异,由于微信没有给出其他语言类型的示例代码,故加上SORT_STRING。通过把timestamp,token,nonce排序sha1加密后得到signature。对于简化验证,可以不进行验证,直接输出echostr,安全性稍差。大部分用户验证后对post数据类型直接改为$wechatObj->responseMsg(),不再对签名进行验证。由于post时仍有上述参数可以仍然调用验证签名,在responseMsg增加调用valid方法,以保证安全性。不过加上此类调用,对于调试就不太方便,微信官方的debug和宾果等均无法返回调试结果,代码如下:
<?php
/**
* wechat php test
*/
//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
if($_SERVER['REQUEST_METHOD']== "GET"){
$wechatObj->valid();
}
elseif($_SERVER['REQUEST_METHOD']== "POST"){
$wechatObj->responseMsg();
}
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
//valid signature , option
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
public function responseMsg()
{
if($this->checkSignature()){
//get post data, May be due to the different environments
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
//extract post data
if (!empty($postStr)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
$time = time();
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
if(!empty( $keyword ))
{
$msgType = "text";
$contentStr = "Welcome to wechat world!";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}else{
echo "Input something...";
}
}else {
echo "";
exit;
}
}
}
private function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}
?>