TP3.2校验微信公众号、小程序 服务器地址

1、在TP3.2里面,写一个控制器,用来校验微信公众号、小程序的服务器地址

1.<?php 
 2.namespace Home\Controller;
 3.use Think\Controller;
 4.header('Content-type:text');
 5.define("TOKEN", "x**");
 6.class XiaoKeFuController extends Controller {
 7.public function index(){
 8.if (isset($_GET['echostr'])) {
 9.$this->valid();
 10.}else{
 11.$this->responseMsg();
 12.}
 13.}
 14.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.}​​​​​​​
 26.private function checkSignature()
 27.{
 28.$signature = $_GET["signature"];
 29.$timestamp = $_GET["timestamp"];
 30.$nonce = $_GET["nonce"];​​​​​​​
 31.$token = TOKEN;
 32.$tmpArr = array($token, $timestamp, $nonce);
 33.sort($tmpArr, SORT_STRING);
 34.$tmpStr = implode( $tmpArr );
 35.$tmpStr = sha1( $tmpStr );​​​​​​​
 36.if( $tmpStr == $signature ){ 
 37.return true;
 38.}else{
 39.return false;
 40.}
 41.}​​​​​​​
 42.public function responseMsg()
 43.{
 44.$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];​​​​​​​
 45.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.}

2、服务器地址URL:

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

PHP原生实现,校验微信公众号||小程序服务器地址

1、原生的.php文件: test.php

1.<?php
 2.header('Content-type:text');
 3.define("TOKEN", "weixin");​​​​​​​
 4.$wechatObj = new wechatCallbackapiTest();
 5.if (isset($_GET['echostr'])) {
 6.$wechatObj->valid();
 7.}else{
 8.$wechatObj->responseMsg();
 9.}​​​​​​​
 10.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.}​​​​​​​
 21.private function checkSignature()
 22.{
 23.$signature = $_GET["signature"];
 24.$timestamp = $_GET["timestamp"];
 25.$nonce = $_GET["nonce"];​​​​​​​
 26.$token = TOKEN;
 27.$tmpArr = array($token, $timestamp, $nonce)
 28.sort($tmpArr, SORT_STRING);
 29.$tmpStr = implode( $tmpArr );
 30.$tmpStr = sha1( $tmpStr );​​​​​​​
 31.if( $tmpStr == $signature ){
 32.return true;
 33.}else{
 34.return false;
 35.}
 36.}​​​​​​​
 37.public function responseMsg()
 38.{
 39.$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];​​​​​​​
 40.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.?>

2、拿去直接用,放在/目录下即可。

公众号服务器地址URL:

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

token:weixin

转载于:https://my.oschina.net/u/3396785/blog/1490100

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值