今天配置了一下下午的token,但是不知道怎么回事就是配置不成功,在网上找了多的方法也没有解决了这个问题,但是最终做在一个论坛上找到了解决的方法。真是激动的不要不要的,话不多说直接怼代码。

  这个是微信公众平台上的配置。

  

  这边的url值要直接点到文件上,不然不会成功.  

然后后台的设置

  1. 1 <?php
  2. 2 namespace app\weixin\controller;
  3. 3
  4. 4 use think\Controller;
  5. 5
  6. 6 define("TOKEN", "******");//这里要输入你的token值
  7. 7
  8. 8 // $wechatObj = new wechatCallbackapiTest();
  9. 9
  10. 10 // $wechatObj->valid();
  11. 11
  12. 12 class Wx2 extends Controller {
  13. 13
  14. 14 public function index()
  15. 15 {
  16. 16 $echoStr = $_GET["echostr"];
  17. 17
  18. 18 //valid signature , option
  19. 19 if($this->checkSignature()){
  20. 20 ob_clean(); //丢弃缓存中的内容
  21. 21 echo $echoStr;
  22. 22 exit;
  23. 23 }
  24. 24 }
  25. 25
  26. 26 public function responseMsg()
  27. 27 {
  28. 28 //get post data, May be due to the different environments
  29. 29 $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
  30. 30
  31. 31 //extract post data
  32. 32 if (!empty($postStr)){
  33. 33
  34. 34 $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  35. 35 $fromUsername = $postObj->FromUserName;
  36. 36 $toUsername = $postObj->ToUserName;
  37. 37 $keyword = trim($postObj->Content);
  38. 38 $time = time();
  39. 39 $textTpl = "<xml>
  40. 40 <ToUserName><![CDATA[%s]]></ToUserName>
  41. 41 <FromUserName><![CDATA[%s]]></FromUserName>
  42. 42 <CreateTime>%s</CreateTime>
  43. 43 <MsgType><![CDATA[%s]]></MsgType>
  44. 44 <Content><![CDATA[%s]]></Content>
  45. 45 <FuncFlag>0</FuncFlag>
  46. 46 </xml>";
  47. 47 if(!empty( $keyword ))
  48. 48 {
  49. 49 $msgType = "text";
  50. 50 $contentStr = "Welcome to wechat world!";
  51. 51 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
  52. 52 echo $resultStr;
  53. 53 }else{
  54. 54 echo "Input something...";
  55. 55 }
  56. 56
  57. 57 }else {
  58. 58 echo "";
  59. 59 exit;
  60. 60 }
  61. 61 }
  62. 62
  63. 63 private function checkSignature()
  64. 64 {
  65. 65 $signature = $_GET["signature"];
  66. 66 $timestamp = $_GET["timestamp"];
  67. 67 $nonce = $_GET["nonce"];
  68. 68
  69. 69 $token = TOKEN;
  70. 70 $tmpArr = array($token, $timestamp, $nonce);
  71. 71 sort($tmpArr);
  72. 72 $tmpStr = implode( $tmpArr );
  73. 73 $tmpStr = sha1( $tmpStr );
  74. 74
  75. 75 if( $tmpStr == $signature ){
  76. 76 return true;
  77. 77 }else{
  78. 78 return false;
  79. 79 }
  80. 80 }
  81. 81
  82. 82
  83. 83 }

  若是这种方法也不行的,只有修改编码方式来执行了,修改编码方式在网上有好多,自己随便一搜索就出来,

  今天就分享到这里,然后觉得我写的不错的请推荐,谢谢

<?phpnamespace app\weixin\controller;
use think\Controller;
define(“TOKEN”, “ouzhouyinxiang666888”);
// $wechatObj = new wechatCallbackapiTest();
// $wechatObj->valid();
class Wx2 extends Controller {public function index()      {          $echoStr = $_GET[“echostr”];            //valid signature , option          if($this->checkSignature()){          ob_clean();            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);          $tmpStr = implode( $tmpArr );          $tmpStr = sha1( $tmpStr );                    if( $tmpStr == $signature ){              return true;          }else{              return false;          }      }

}

版权声明:本文为CcPz原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/CcPz/p/9437933.html