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 = "
52.
53.
54.%s
55.
56.
57.0
58.";
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 = "
47.
48.
49.%s
50.
51.
52.0
53.";
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