PHP 钉钉机器人

php通过钉钉机器人发送消息

function sendMessage2DingTalk($msg, $secret, $accessToken)
{
    // 当前时间(毫秒)
    $timestamp = time() * 1000;
    // HmacSHA256加密,设置为 true 输出原始二进制数据
    $hmacsha256 = hash_hmac('sha256', $timestamp . "\n" . $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\r\n",
            // 请求内容
            '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");
posted @ 2021-03-01 15:46  凭栏知潇雨  阅读(435)  评论(0编辑  收藏  举报