钉钉群机器人群发[ PHP ]

// secret  机器人设置 - 加签秘钥
// access_token  机器人设置 - Webhook带此参数
// message  机器人设置- 关键词设置的内容需要和message一致

public function usercheck(){
        
    list($s1, $s2) = explode(' ', microtime());

    $timestamp = (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
    
    $secret = 'xxxx';//机器人设置-加签秘钥

    $data = $timestamp . "\n" . $secret;

    $signStr = base64_encode(hash_hmac('sha256', $data, $secret,true));

    $signStr = utf8_encode(urlencode($signStr));
    
    
    $webhook = "https://oapi.dingtalk.com/robot/send?access_token=xxxx&timestamp=".$timestamp."&sign=".$signStr;
    $message="测试";
    $data = array ('msgtype' => 'text','text' => array ('content' => $message));
    $data_string = json_encode($data);

    $result = $this->request_by_curl($webhook, $data_string);
    
    
}

public function request_by_curl($remote_server, $post_string) {  
    
    $ch = curl_init();  
    curl_setopt($ch, CURLOPT_URL, $remote_server);
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Content-Type: application/json;charset=utf-8'));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
    // 线下环境不用开启curl证书验证, 未调通情况可尝试添加该代码
    curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0); 
    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $data = curl_exec($ch);
    curl_close($ch);        
    return $data;  
}
View Code

转自:https://blog.csdn.net/weixin_29821223/article/details/115563818

官方文档:https://open.dingtalk.com/document/robots/custom-robot-access

 

---------------

posted on 2022-08-24 17:29  逝年-  阅读(63)  评论(0编辑  收藏  举报