微信公众号基本配置
今天配置了一下下午的token,但是不知道怎么回事就是配置不成功,在网上找了多的方法也没有解决了这个问题,但是最终做在一个论坛上找到了解决的方法。真是激动的不要不要的,话不多说直接怼代码。
这个是微信公众平台上的配置。
这边的url值要直接点到文件上,不然不会成功.
然后后台的设置
- 1 <?php
- 2 namespace app\weixin\controller;
- 3
- 4 use think\Controller;
- 5
- 6 define("TOKEN", "******");//这里要输入你的token值
- 7
- 8 // $wechatObj = new wechatCallbackapiTest();
- 9
- 10 // $wechatObj->valid();
- 11
- 12 class Wx2 extends Controller {
- 13
- 14 public function index()
- 15 {
- 16 $echoStr = $_GET["echostr"];
- 17
- 18 //valid signature , option
- 19 if($this->checkSignature()){
- 20 ob_clean(); //丢弃缓存中的内容
- 21 echo $echoStr;
- 22 exit;
- 23 }
- 24 }
- 25
- 26 public function responseMsg()
- 27 {
- 28 //get post data, May be due to the different environments
- 29 $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
- 30
- 31 //extract post data
- 32 if (!empty($postStr)){
- 33
- 34 $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
- 35 $fromUsername = $postObj->FromUserName;
- 36 $toUsername = $postObj->ToUserName;
- 37 $keyword = trim($postObj->Content);
- 38 $time = time();
- 39 $textTpl = "<xml>
- 40 <ToUserName><![CDATA[%s]]></ToUserName>
- 41 <FromUserName><![CDATA[%s]]></FromUserName>
- 42 <CreateTime>%s</CreateTime>
- 43 <MsgType><![CDATA[%s]]></MsgType>
- 44 <Content><![CDATA[%s]]></Content>
- 45 <FuncFlag>0</FuncFlag>
- 46 </xml>";
- 47 if(!empty( $keyword ))
- 48 {
- 49 $msgType = "text";
- 50 $contentStr = "Welcome to wechat world!";
- 51 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
- 52 echo $resultStr;
- 53 }else{
- 54 echo "Input something...";
- 55 }
- 56
- 57 }else {
- 58 echo "";
- 59 exit;
- 60 }
- 61 }
- 62
- 63 private function checkSignature()
- 64 {
- 65 $signature = $_GET["signature"];
- 66 $timestamp = $_GET["timestamp"];
- 67 $nonce = $_GET["nonce"];
- 68
- 69 $token = TOKEN;
- 70 $tmpArr = array($token, $timestamp, $nonce);
- 71 sort($tmpArr);
- 72 $tmpStr = implode( $tmpArr );
- 73 $tmpStr = sha1( $tmpStr );
- 74
- 75 if( $tmpStr == $signature ){
- 76 return true;
- 77 }else{
- 78 return false;
- 79 }
- 80 }
- 81
- 82
- 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; } }
}