forked from easychen/wecomchan
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
133 lines (115 loc) · 3.82 KB
/
index.php
File metadata and controls
133 lines (115 loc) · 3.82 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
// ======================================
// config
// ======================================
define('SENDKEY', 'set_a_sendkey');
define('WECOM_CID', '企业微信公司ID');
define('WECOM_SECRET', '企业微信应用Secret');
define('WECOM_AID', '企业微信应用ID');
define('WECOM_TOUID', '@all');
// Redis config
define('REDIS_ON', true);
define('REDIS_HOST', '127.0.0.1');
define('REDIS_PORT', 6379);
define('REDIS_EXPIRED', 7000);
define('REDIS_PASSWORD', '');
define('REDIS_KEY', 'wecom_access_token');
// ======================================
// helper functions
// ======================================
function safe_param($arr, $key) {
return isset($arr[$key]) ? trim((string)$arr[$key]) : '';
}
function redis()
{
static $instance = null;
if ($instance === null) {
$instance = new Redis();
$instance->connect(REDIS_HOST, REDIS_PORT);
if (REDIS_PASSWORD !== '') {
$instance->auth(REDIS_PASSWORD);
}
}
return $instance;
}
// ======================================
// main logic
// ======================================
$method = $_SERVER['REQUEST_METHOD'] ?? 'GET';
$input_data = [];
// ✅ 如果是 JSON 请求
if ($method === 'POST' && str_contains($_SERVER['CONTENT_TYPE'] ?? '', 'application/json')) {
$raw = file_get_contents('php://input');
$json = json_decode($raw, true);
if (is_array($json)) {
$input_data = $json;
}
} elseif ($method === 'POST') {
$input_data = $_POST;
} elseif ($method === 'GET') {
$input_data = $_GET;
} else {
die('Method not supported');
}
// 兼容 text/msg 参数
$sendkey = safe_param($input_data, 'sendkey');
$msg = safe_param($input_data, 'msg') ?: safe_param($input_data, 'text');
// 参数验证
if ($sendkey !== SENDKEY || $msg === '') {
die('bad params');
}
// 发消息
header("Content-Type: application/json; charset=UTF-8");
echo send_to_wecom($msg, WECOM_CID, WECOM_SECRET, WECOM_AID, WECOM_TOUID);
// ======================================
// send function
// ======================================
function send_to_wecom($text, $wecom_cid, $wecom_secret, $wecom_aid, $wecom_touid = '@all')
{
$access_token = false;
// 从 Redis 获取缓存
if (REDIS_ON) {
$access_token = redis()->get(REDIS_KEY);
}
if (!$access_token) {
$url_token = sprintf(
'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s',
urlencode($wecom_cid),
urlencode($wecom_secret)
);
$info = json_decode(@file_get_contents($url_token), true);
if (!empty($info['access_token'])) {
$access_token = $info['access_token'];
}
}
if ($access_token) {
$url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' . urlencode($access_token);
$data = [
"touser" => $wecom_touid,
"agentid" => $wecom_aid,
"msgtype" => "text",
"text" => ["content" => $text],
"duplicate_check_interval" => 600
];
$data_json = json_encode($data, JSON_UNESCAPED_UNICODE);
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $data_json,
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_TIMEOUT => 5,
]);
$response = curl_exec($ch);
curl_close($ch);
// 缓存 access_token
if ($response !== false && REDIS_ON && $access_token) {
redis()->set(REDIS_KEY, $access_token, ['nx', 'ex' => REDIS_EXPIRED]);
}
return $response;
}
return json_encode(['error' => 'failed to get access token']);
}