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

来源:互联网 发布:查日本经济数据的网站 编辑:程序博客网 时间:2024/06/05 18:16

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

1.<?php  2.namespace Home\Controller3.use Think\Controller4.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.}else11.$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.exit21.}else22.echo $echoStr.'+++'.TOKEN; 23.exit24.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 true38.}else39.return false40.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.}else67.echo ""68.exit69.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.}else8.$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.exit19.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 true33.}else34.return false35.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.}else62.echo ""63.exit64.65.66.67.?>

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

公众号服务器地址URL:

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

token:weixin

阅读全文
0 0