PHP微信公众号开发(一) 配置服务器token验证

2023-09-24 13 0

  • 使用该php文件进行验证的前提是,已经在公众号平台设置服务器配置,并将如:sdfsdfd.txt 放入网站根目录下
<?php
/*** wechat php test*///define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();class wechatCallbackapiTest
{public function valid(){$echoStr = $_GET["echostr"];//valid signature , optionif($this->checkSignature()){echo $echoStr;exit;}}public function responseMsg(){//get post data, May be due to the different environments$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];//extract post dataif (!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);$tmpStr = implode( $tmpArr );$tmpStr = sha1( $tmpStr );if( $tmpStr == $signature ){return true;}else{return false;}}
}?>
代码编程
赞赏

相关文章

IOC容器——Unity 深度解析(一)
IOC容器——Unity
c#——标准Dispose模式的实现
c#——表达式树在LINQ动态查询
c#——ExpessionTree(表达式树)
c#—— Task.FromResult 的使用