[PHP] 微信token认证程序 →→→→→进入此内容的聊天室

来自 , 2020-02-08, 写在 PHP, 查看 105 次.
URL http://www.code666.cn/view/f2bff080
  1. <?php
  2. /**
  3.   * @author heqing
  4.   * @version v1.0
  5.   */
  6.  
  7.  
  8. //define your token
  9. define("TOKEN", "weixin");//这里填写的是你在微信上设置的TOKEN,但是必须保证与微信公众平台 接口配置信息一致
  10. $wechatObj = new wechatCallbackapiTest();
  11. $wechatObj->valid();//这里是测试网站配置信息和开发的是否一致。
  12.  
  13. $wechatObj->responseMsg();//新增加这一项,目的是调用responseMsg()这个功能。
  14.  
  15.  
  16.  
  17. class wechatCallbackapiTest
  18. {
  19.  
  20. //若确认此次GET请求来自微信服务器,请原样返回echostr参数内容,则接入生效,否则接入失败。
  21.  
  22. public function valid()
  23.     {
  24.         $echoStr = $_GET["echostr"];
  25.  
  26.  
  27.         //valid signature , option
  28.         if($this->checkSignature()){
  29.                 header('content-type:text');
  30.         echo $echoStr;
  31.         exit;
  32.         }
  33.     }
  34.  
  35.  
  36.     public function responseMsg()
  37.     {
  38. //get post data, May be due to the different environments
  39. $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
  40.  
  41.  
  42.       //extract post data
  43. if (!empty($postStr)){
  44.                  
  45.               $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  46.                 $fromUsername = $postObj->FromUserName;
  47.                 $toUsername = $postObj->ToUserName;
  48.                 $keyword = trim($postObj->Content);
  49.                 $time = time();
  50.                 $textTpl = "<xml>
  51. <ToUserName><![CDATA[%s]]></ToUserName>
  52. <FromUserName><![CDATA[%s]]></FromUserName>
  53. <CreateTime>%s</CreateTime>
  54. <MsgType><![CDATA[%s]]></MsgType>
  55. <Content><![CDATA[%s]]></Content>
  56. <FuncFlag>0</FuncFlag>
  57. </xml>";              
  58. if(!empty( $keyword ))// $keyword是关键词,即为用户发送的信息
  59.                 {
  60.                 $msgType = "text";//文本格式的消息
  61.                 $contentStr = "Welcome to wechat world!";//预制消息回复
  62.                
  63.                 if ($keyword == "index") {
  64.                 $contentStr = "welcome to nongdaxy.com";
  65.                 }
  66.                
  67.                 if ($keyword == "首页") {
  68.                 $contentStr = "欢迎来到校园之窗";
  69.                 }
  70.                 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
  71.                 echo $resultStr;
  72.                 }else{
  73.                 echo "Input something...";
  74.                 }
  75.  
  76.  
  77.         }else {
  78.         echo "";
  79.         exit;
  80.         }
  81.     }
  82. // 开发者通过检验signature对请求进行校验
  83. private function checkSignature()
  84. {
  85.         $signature = $_GET["signature"];
  86.         $timestamp = $_GET["timestamp"];
  87.         $nonce = $_GET["nonce"];
  88.        
  89. $token = TOKEN;
  90. $tmpArr = array($token, $timestamp, $nonce);
  91. sort($tmpArr);
  92. $tmpStr = implode( $tmpArr );
  93. $tmpStr = sha1( $tmpStr );
  94.  
  95. if( $tmpStr == $signature ){
  96. return true;
  97. }else{
  98. return false;
  99. }
  100. }
  101. }
  102.  
  103.  
  104. ?>

回复 "微信token认证程序"

这儿你可以回复上面这条便签

captcha