微信公众平台搭建
发布时间:2020-12-16 09:39:07 所属栏目:百科 来源:网络整理
导读:?php/** * wechat php test *///define your tokendefine("TOKEN","weixin");$wechatObj = new wechatCallbackapiTest();$wechatObj-valid();class wechatCallbackapiTest{public function valid() { $echoStr = $_GET["echostr"]; //valid signature,option
<?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,option if($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 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; } } } ?> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
推荐文章
站长推荐
- 关于Flashback table tablename to before drop的
- SQLite的学习总结
- PostgreSQL数据库中的表和数据(Tables & Da
- ruby-on-rails – 安装rails时找不到Gem存储库
- c – uint8_t的类型乘以布尔值是什么?
- 领域驱动设计(DDD:Domain-Driven Design)
- ruby-on-rails – Rails PostgreSQL按字符串的整
- oracle中delete、truncate、drop的区别
- oracle – 如何重新处理异常处理块中的pl / sql异
- React-Redux小应用(一)-React_Redux_Appointme
热点阅读