<?php
define ( "TOKEN", "wxsflsgfs" );
function checkSignature() {
$signature = $_GET ['signature'];
$nonce = $_GET ['nonce'];
$timestamp = $_GET ['timestamp'];
$tmpArr = array ($nonce, $timestamp, TOKEN );
sort ( $tmpArr );
$tmpStr = implode ( $tmpArr );
$tmpStr = sha1 ( $tmpStr );
if ($tmpStr == $signature) {
return true;
}
return false;
}
if(false == checkSignature()) {
exit(0);
}
$echostr = $_GET ['echostr'];
/*if(true == checkSignature()) {
if($echostr) {
echo $echostr;
exit(0);
}
} //在验证token时,即提交微信服务器验证时打开,并注释掉以后所有程序,验证后再切换
//因为下一if语句也可以不这么麻烦
*/
if($echostr) {
echo $echostr;
exit(0);
}
function getPostData() {
$data = $GLOBALS['HTTP_RAW_POST_DATA'];
return $data;
}
$PostData = getPostData();
if(!$PostData){
echo_server_log("wrong input! PostData is NULL");
echo "wrong input!";
exit(0);
}
$xmlObj = simplexml_load_string($PostData, 'SimpleXMLElement', LIBXML_NOCDATA);
if(!$xmlObj) {
echo_server_log("wrong input! xmlObj is NULL\n");
echo "wrong input!";
exit(0);
}
$fromUserName = $xmlObj->FromUserName;
$toUserName = $xmlObj->ToUserName;
$msgType = $xmlObj->MsgType;
//以下位guanfs调试用代码
$keyword =trim($xmlObj->Content);
$MsgId =$xmlObj->MsgId; //消息内容的随机id
$CreateTime = time();//$xmlObj->Createtime;//intval()
$formtime =date("Y-m-d H:i:s",$CreateTime);
$time=time();
$retTmp = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
//查看消息内容
if('text' != $msgType) {
$retMsg = '只支持文本消息';
}else{
$mymsg ="微信服务号ID:".$toUserName."\n";
$mymsg .="手机用户ID:".$fromUserName."\n";
$mymsg .="消息发送时间戳:".$CreateTime."\n";
$mymsg .="消息类型:".$msgType."\n";
$mymsg .="消息内容:".$keyword."\n";
$mymsg .="消息ID:".$MsgId."\n";
$retMsg = $mymsg;
}
$resultStr = sprintf($retTmp, $fromUserName, $toUserName,time(),$retMsg);
echo $resultStr
?>
具体看代码吧,保存为echoserver.php既可以使用,手机上会收到相关返回信息。微信的接入验证和其他开发的功能如群发模板消息的功能没有必然联系,可以放在不同文件中。
微信 接入验证 基本回复 自己openid获得方法
最新推荐文章于 2024-08-22 18:04:28 发布