/
zinterface.php
104 lines (85 loc) · 2.73 KB
/
zinterface.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
require_once(dirname(__FILE__) . '/wxmp/common/GlobalDefine.php');
require_once(dirname(__FILE__) . '/wxmp/common/GlobalFunctions.php');
require_once(dirname(__FILE__) . '/wxmp/common/Common.php');
//验证token
function checkSignature() {
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = WEIXIN_TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr);
$tmpStr = implode($tmpArr);
$tmpStr = sha1($tmpStr);
if($tmpStr == $signature) {
return true;
} else {
return false;
}
}
//if(false == checkSignature()){
// exit(0);
//}
//$echoStr = $_GET["echostr"];
//valid signature, option
if(checkSignature()){
echo $_GET["echostr"];
exit(0);
}
function getWeChatObj($toUserName) {
if($toUserName == USERNAME_JSZL) {
require_once ROOT_PATH . '/class/WeChatCallBackJSZL.php';
return new WeChatCallBackJSZL();
}
require_once ROOT_PATH . '/class/WeChatCallBack.php';
//interface_log(INFO, EC_OK, 'require成功:');
return new WeChatCallBack();
}
function exitErrorInput(){
echo 'error input!';
interface_log(INFO, EC_OK, "***** interface request end *****");
interface_log(INFO, EC_OK, "*********************************");
interface_log(INFO, EC_OK, "");
exit ( 0 );
}
$postStr = file_get_contents("php://input");
if (empty($postStr)) {
interface_log ( ERROR, EC_OK, "error input!" );
exitErrorInput();
}
interface_log(INFO, EC_OK, "");
interface_log(INFO, EC_OK, "***********************************");
interface_log(INFO, EC_OK, "***** interface request start *****");
interface_log(INFO, EC_OK, 'request:' . $postStr);
interface_log(INFO, EC_OK, 'get:' . var_export($_GET, true));
// 获取参数
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
if(NULL == $postObj) {
interface_log(ERROR, 0, "can not decode xml");
exit(0);
}
$content=$postObj->Content;
interface_log(INFO, EC_OK, '内容:' . $content);
$toUserName = (string)trim($postObj->ToUserName);
if (!$toUserName) {
interface_log ( ERROR, EC_OK, "error input!" );
exitErrorInput();
} else {
$wechatObj = getWeChatObj($toUserName);
}
$ret = $wechatObj->init($postObj);
interface_log(INFO, EC_OK, 'init($postObj):' . $ret);
if (! $ret) {
interface_log ( ERROR, EC_OK, "error input!" );
exitErrorInput();
}
$retStr = $wechatObj->process ();
interface_log ( INFO, EC_OK, "response:" . $retStr );
echo $retStr;
interface_log(INFO, EC_OK, "***** interface request end *****");
interface_log(INFO, EC_OK, "*********************************");
interface_log(INFO, EC_OK, "");
$useTime = microtime(true) - $startTime;
interface_log ( INFO, EC_OK, "cost time:" . $useTime . " " . ($useTime > 4 ? "warning" : "") );
?>