php通过钉钉机器人发送消息
function sendMessage2DingTalk($msg, $secret, $accessToken)
{
// 当前时间(毫秒)
$timestamp = time() * 1000;
// HmacSHA256加密,设置为 true 输出原始二进制数据
$hmacsha256 = hash_hmac('sha256', $timestamp . "
" . $secret, $secret, true);
// 请求参数
$param = [
'access_token' => $accessToken,
'timestamp' => $timestamp,
// 签名值
'sign' => urlencode(base64_encode($hmacsha256))
];
// Webhook地址
$webhook = "https://oapi.dingtalk.com/robot/send?" . http_build_query($param);
// 发送消息
$options = [
'http' => [
// 请求方法
'method' => "POST",
// 请求格式为json
'header' => "Content-type:application/json;charset=utf-8
",
// 请求内容
'content' => $msg
],
// 不验证ssl证书
"ssl" => [
"verify_peer" => false,
"verify_peer_name" => false
]
];
return file_get_contents($webhook, false, stream_context_create($options));
}
sendMessage2DingTalk('{"msgtype":"text","text":{"content":"我就是我, 是不一样的烟火啊"}}', "SECxxx", "xxx");