TP3.2校驗微信公衆號、小程序 服務器地址

一、在TP3.2裏面,寫一個控制器,用來校驗微信公衆號、小程序的服務器地址php

 6.
 14.​​​​​​​
 26.​​​​​​​
 31.​​​​​​​
 36.​​​​​​​
 42.​​​​​​​
 45.1.<?php 
 2.namespace Home\Controller;
 3.use Think\Controller;
 4.header('Content-type:text');
 5.define("TOKEN", "x**");class XiaoKeFuController extends Controller {
 7.public function index(){
 8.if (isset($_GET['echostr'])) {
 9.$this->valid();
 10.}else{
 11.$this->responseMsg();
 12.}
 13.}public function valid()
 15.{
 16.$echoStr = $_GET["echostr"];
 17.if($this->checkSignature()){
 18.header('content-type:text');
 19.echo $echoStr;
 20.exit;
 21.}else{
 22.echo $echoStr.'+++'.TOKEN;
 23.exit;
 24.}
 25.}private function checkSignature()
 27.{
 28.$signature = $_GET["signature"];
 29.$timestamp = $_GET["timestamp"];
 30.$nonce = $_GET["nonce"];$token = TOKEN;
 32.$tmpArr = array($token, $timestamp, $nonce);
 33.sort($tmpArr, SORT_STRING);
 34.$tmpStr = implode( $tmpArr );
 35.$tmpStr = sha1( $tmpStr );if( $tmpStr == $signature ){ 
 37.return true;
 38.}else{
 39.return false;
 40.}
 41.}public function responseMsg()
 43.{
 44.$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];if (!empty($postStr)){
 46.$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
 47.$fromUsername = $postObj->FromUserName;
 48.$toUsername = $postObj->ToUserName;
 49.$keyword = trim($postObj->Content);
 50.$time = time();
 51.$textTpl = "<xml>
 52.<ToUserName><![CDATA[%s]]></ToUserName>
 53.<FromUserName><![CDATA[%s]]></FromUserName>
 54.<CreateTime>%s</CreateTime>
 55.<MsgType><![CDATA[%s]]></MsgType>
 56.<Content><![CDATA[%s]]></Content>
 57.<FuncFlag>0</FuncFlag>
 58.</xml>";
 59.if($keyword == "?" || $keyword == "?")
 60.{
 61.$msgType = "text";
 62.$contentStr = date("Y-m-d H:i:s",time());
 63.$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
 64.echo $resultStr;
 65.}
 66.}else{
 67.echo "";
 68.exit;
 69.}
 70.}
 71.}

二、服務器地址URL:小程序

https://m.*.com/index.php/Home/XiaoKeFu/index 
token:x**api

PHP原生實現,校驗微信公衆號||小程序服務器地址

一、原生的.php文件: test.php服務器

​​​​​​​
 4.​​​​​​​
 10.​​​​​​​
 21.​​​​​​​
 26.​​​​​​​
 31.​​​​​​​
 37.​​​​​​​
 40.1.<?php
 2.header('Content-type:text');
 3.define("TOKEN", "weixin");$wechatObj = new wechatCallbackapiTest();
 5.if (isset($_GET['echostr'])) {
 6.$wechatObj->valid();
 7.}else{
 8.$wechatObj->responseMsg();
 9.}class wechatCallbackapiTest
 11.{
 12.}public function valid()
 13.{
 14.$echoStr = $_GET["echostr"];
 15.if($this->checkSignature()){
 16.header('content-type:text');
 17.echo $echoStr;
 18.exit;
 19.}
 20.}private function checkSignature()
 22.{
 23.$signature = $_GET["signature"];
 24.$timestamp = $_GET["timestamp"];
 25.$nonce = $_GET["nonce"];$token = TOKEN;
 27.$tmpArr = array($token, $timestamp, $nonce)
 28.sort($tmpArr, SORT_STRING);
 29.$tmpStr = implode( $tmpArr );
 30.$tmpStr = sha1( $tmpStr );if( $tmpStr == $signature ){
 32.return true;
 33.}else{
 34.return false;
 35.}
 36.}public function responseMsg()
 38.{
 39.$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];if (!empty($postStr)){
 41. $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
 42.$fromUsername = $postObj->FromUserName;
 43.$toUsername = $postObj->ToUserName;
 44.$keyword = trim($postObj->Content);
 45.$time = time();
 46.$textTpl = "<xml>
 47.<ToUserName><![CDATA[%s]]></ToUserName>
 48.<FromUserName><![CDATA[%s]]></FromUserName>
 49.<CreateTime>%s</CreateTime>
 50.<MsgType><![CDATA[%s]]></MsgType>
 51.<Content><![CDATA[%s]]></Content>
 52.<FuncFlag>0</FuncFlag>
 53.</xml>";
 54.if($keyword == "?" || $keyword == "?")
 55.{
 56.$msgType = "text";
 57.$contentStr = date("Y-m-d H:i:s",time());
 58.$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
 59.echo $resultStr;
 60.}
 61.}else{
 62.echo "";
 63.exit;
 64.}
 65.}
 66.}
 67.?>

二、拿去直接用,放在/目錄下便可。微信

公衆號服務器地址URL:post

https://m.****.com/test.phpthis

token:weixinspa

相關文章
相關標籤/搜索