forked from kaixin1995/InformationPush
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdingtalk.php
More file actions
59 lines (50 loc) · 1.44 KB
/
dingtalk.php
File metadata and controls
59 lines (50 loc) · 1.44 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
<?php
/**
* =========================
* 钉钉机器人配置区
* =========================
*/
$access_token = "Access_token"; // access_token
$api_base_url = "https://oapi.dingtalk.com/robot/send"; // 钉钉 API,可改为反代
// 如果不存在文本就禁止提交
if (!isset($_REQUEST['msg'])) {
exit;
}
// curl请求函数
function https_request($url, $data) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json;charset=utf-8'));
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
/**
* =========================
* 开始推送
* =========================
*/
// 构造完整 Webhook URL(拼接路径)
$webhook = $api_base_url . "?access_token=" . $access_token;
// 获取消息内容
$message = $_REQUEST['msg'];
// 构造请求数据
$data = [
'msgtype' => 'text',
'text' => [
'content' => $message
]
];
// 发送请求
$json_data = json_encode($data, JSON_UNESCAPED_UNICODE);
$result = https_request($webhook, $json_data);
// 解析响应
$response = json_decode($result, true);
if ($response && $response['errcode'] == 0) {
echo "Success";
} else {
echo "Error: " . $result; // 原始响应,方便调试
}