Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

我写了个钉钉通知的class,我测试可用。作者可以加进去 #18

Open
lalblc opened this issue Dec 28, 2022 · 0 comments
Open

Comments

@lalblc
Copy link

lalblc commented Dec 28, 2022

我也是个小白,边百度边写的。
钉钉可用在不运行钉钉的时候发送离线通知,消息比较及时,感觉可用加一下。

<?php


class Push{
    // 钉钉-加签
    protected static function ding_url($str = ''){
        // 钉钉给出的密钥(加签用的)
        $secret='xxxxxxxxx';  //要自己添加
        // 钉钉给的token
        $token='xxxxxxxxx';  //要自己添加
        

        // 获取微秒数时间戳
        $Temptime = explode(' ', microtime());
        // 转换成毫秒数时间戳
        $msectime = (float)sprintf('%.0f', (floatval($Temptime[0]) + floatval($Temptime[1])) * 1000);
        // 拼装成待加密字符串
        // 格式:毫秒数+"\n"+密钥
        $stringToSign=$msectime."\n".$secret;
        // 进行加密操作 并输出二进制数据
        $sign=hash_hmac('sha256', $stringToSign,$secret,true);
        // 加密后进行base64编码 以及url编码
        $sign=urlencode(base64_encode($sign));
        // 拼接url
        $url='https://oapi.dingtalk.com/robot/send?access_token='.$token;
        $url.='&timestamp='.$msectime; // 拼接时间戳
        $url.='&sign='.$sign;          // 拼接加密签名
        return $url;
    }


    //发送post请求
    protected static function post($url,$arr=[]) {    
        $data = json_encode($arr);
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_exec($curl);
        curl_close($curl);
    }

    public static function send_text($text="发送了空白消息"){
        $data = [
            "text"=> [
                "content"=>$text
            ],
            "msgtype" => "text"
        ];
        Push::post(Push::ding_url(),$data);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant