1. 程式人生 > >TP3.2校驗微信公眾號、小程式 伺服器地址

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.classXiaoKeFuControllerextendsController{
 7.public function index(){
 8.if (isset($_GET['echostr'])) {
 9.$this->valid();
 10.}else
{  11.$this->responseMsg();  12.}  13.}
 14.public functionvalid()  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 functioncheckSignature
()  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 functionresponseMsg()  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.classwechatCallbackapiTest
 11.{
 12.}publicfunctionvalid()
 13.{
 14.$echoStr = $_GET["echostr"];
 15.if($this->checkSignature()){
 16.header('content-type:text');
 17.echo $echoStr;
 18.exit;
 19.}
 20.}​​​​​​​
 21.privatefunctioncheckSignature()
 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.returntrue;
 33.}else{
 34.returnfalse;
 35.}
 36.}​​​​​​​
 37.publicfunctionresponseMsg()
 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