关于钉钉发送普通消息的接口操作
假如说我们提交一个操作,要告诉钉钉里的某一个人,我已经提交了,你审核一下,这个时候就要发送一条消息给他,就要用到这个操作了
直接上代码:
if ($data){//判断是否提交了一个审核 $corpid="xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; $corpsecret="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; $userlist=array(); $url="https://oapi.dingtalk.com/gettoken?corpid=".$corpid."&corpsecret=".$corpsecret;//获取access_token的方法 $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $output=curl_exec($ch); curl_close($ch); // dump($output);die; $out_array = json_decode($output,true); $access_token = $out_array["access_token"]; $url="https://oapi.dingtalk.com/department/list?access_token=".$access_token; $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $output=curl_exec($ch); $out_array = json_decode($output,true); //var_dump($output); foreach ($out_array["department"] as $DPID){ //echo $DPID["id"]; //echo "\n"; $url="https://oapi.dingtalk.com/user/list?access_token=".$access_token."&department_id=".$DPID["id"]; $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $output=curl_exec($ch); $out_array = json_decode($output,true); //var_dump($out_array); foreach ($out_array["userlist"] as $USR){ $userlist[$USR["mobile"]]=$USR["userid"]; //$tem_array('$USR["mobile"]'=>'$USR["userid"]'); //array_merge($userlist,$tmp_array]); } } // var_dump($userlist);读取该公司的员工的电话和userid,发送短信要用到这个 //发送消息 $url="https://oapi.dingtalk.com/message/send?access_token=".$access_token; $post_array=array("touser" => "0558074762****981","agentid" => "133***7864", "msgtype" => "text", "text" => array("content" => "由出纳".$users['user_login']."审核完成,请登录中心后台审核 !". date("Y-m-d H:i:s",time()))); //如果要给几个人一起发送消息 "touser" => "0558****2999981 | 05580****62999981 ",中间加一个竖线就好了
//touser是员工的userid,agentid是微应用的id,这个id在工作台随便点一个应用的设置,就可以看到,最好看公告的id,毕竟是发消息嘛,texe里面的就是发送的内容了 $post_string=json_encode($post_array); $ch = curl_init(); curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($post_string)) ); $output = curl_exec($ch); curl_close($ch); }
获取
corpid需要登录钉钉企业账号,然后点击工作台,再点击应用开发
corpsecret值的获取也在应用开发里找
所以上述的代码,你只需要填写
corpid corpsecret touser agentid texe 这几个就可以发送消息了.
注:生成CorpSecret的时候关联的部门最好选择整个公司,如果选择某一个部门的话,可能会提示权限不够
看完我代码的备注那你就会发送短信了
人生得意须尽欢,莫使金樽空对月.