获取钉钉accessToken

$appkey_qiye='dinggrxo6a8ejjfg';
$appsecret_qiye='N6skLHsA-18t0tCnvvZkqpVo42TmsXGVT6l1IAVy1zTES-HJEEk';
$url='https://oapi.dingtalk.com/gettoken?appkey='.$appkey_qiye.'&appsecret='.$appsecret_qiye;

$ch = curl_init(); 
 curl_setopt($ch, CURLOPT_URL, $url); 
   //return the transfer as a string 
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
   // $output contains the output string 
   $result = curl_exec($ch); 
   //echo output
  // echo '<br><br>'.$result.'<br>';
   // close curl resource to free up system resources 
   curl_close($ch);
//echo '<br>----<br>';
//var_dump($result);
$accessToken_array=json_decode($result,true);
//echo '<br>----<br>';
//var_dump($accessToken_array);
//$accessToken_array=$result;
$accessToken=$accessToken_array['access_token'];

获取unionid(如通过钉钉扫码获取的tmpcode)

//////////////////////////获取unionid
include "../TopSdk.php";
$c = new DingTalkClient(DingTalkConstant::$CALL_TYPE_OAPI, DingTalkConstant::$METHOD_POST , DingTalkConstant::$FORMAT_JSON);
$req = new OapiSnsGetuserinfoBycodeRequest;
$req->setTmpAuthCode($tmpcode);
$resp=$c->executeWithAccessKey($req, "https://oapi.dingtalk.com/sns/getuserinfo_bycode",$appid,$appsecret);
echo '<br>';
/*
{"errcode":0,"errmsg":"ok","user_info":{"nick":"\u5170\u5f3a","unionid":"hh0LIRUoydeY09l3ouMeg","dingId":"$:LWCP_v1:$\/KVBpujnTsD+gK9n3g==","openid":"UmDpw5YNRSuDiPjs6Bkdr","main_org_auth_high_level":true}}
nick用户在钉钉上面的昵称
openid用户在当前开放应用内的唯一标识
unionid用户在当前开放应用所属企业内的唯一标识
*/

//$resp=json_encode($resp);
var_dump($resp);
echo '<br><br><br>';
$unionid=$resp->user_info->unionid;

根据unionid获取企业userid,需要先得到accessToken

//unionid 与accesstoken 获取userid,需要先得到accessToken
//注意accesstoken获取的appkey  与appsecret 同web扫码登陆的appId  appSecret不同
//include('TopSdk.php');
$c = new DingTalkClient(DingTalkConstant::$CALL_TYPE_OAPI, DingTalkConstant::$METHOD_GET , DingTalkConstant::$FORMAT_JSON);
$req = new OapiUserGetUseridByUnionidRequest;
$req->setUnionid($unionid);
$resp=$c->execute($req,$accessToken, "https://oapi.dingtalk.com/user/getUseridByUnionid");
echo '<br><br>userid<br>';
var_dump($resp);
/*
{
    "errcode": 0,
    "errmsg": "ok",
    "contactType": 0,
    "userid": "userid1"
}
*/
//////////////////////unionid 与accesstoken 获取userid 

根据userid获取用户信息

//获取用户信息
//echo $accessToken;
// DingTalkConstant::$METHOD_GET 要与下面调用接口url要求的保持一致
$c = new DingTalkClient(DingTalkConstant::$CALL_TYPE_OAPI, DingTalkConstant::$METHOD_GET , DingTalkConstant::$FORMAT_JSON);
$req = new OapiUserGetRequest();
$req->setUserid($userid);// 用户 id
$resp=$c->execute($req, $accessToken,"https://oapi.dingtalk.com/user/get");
echo '<br><br>用户返回信息:';
var_dump($resp);

获取部门列表信息

include('TopSdk.php');
//获取部门列表信息
//echo $accessToken;
// DingTalkConstant::$METHOD_GET 要与下面调用接口url要求的保持一致
$c = new DingTalkClient(DingTalkConstant::$CALL_TYPE_OAPI, DingTalkConstant::$METHOD_GET , DingTalkConstant::$FORMAT_JSON);
$req = new OapiDepartmentListRequest();
$req->seId('');// 用户 id
$resp=$c->execute($req, $accessToken,"https://oapi.dingtalk.com/department/list");
echo '<br><br>部门列表返回信息:';
var_dump($resp);

利用应用agentid发送用户消息通知(普通、markdown、oa)

$c = new DingTalkClient(DingTalkConstant::$CALL_TYPE_OAPI, DingTalkConstant::$METHOD_POST , DingTalkConstant::$FORMAT_JSON);
$req = new OapiMessageCorpconversationAsyncsendV2Request();
//$req->setUseridList($content_arr["userid"]);// 必填,与offset和limit配合使用
$req->setUseridList($userid);//userid_list
$req->setAgentId($agentId);
//$req->setToAllUser(false);
//$mms='{"msgtype":"text","text":{"content":"消息内容"}}';
//$req->setMsg($mms);
//$msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
//$msg->setMsgtype("text");
//$msg->setText(new OapiMessageCorpconversationAsyncsendV2Request.Text());
//$msg->getText().setContent("test123");
$mms=array("msgtype"=>"text",
           "text"=>array("content"=>$user_info->name.',您好! 您已通过钉钉扫码WEB管理系统。  '.date("Y-m-d H:i:s")." &".rand(10000,99999))
          );
//$mms='{"msgtype":"text","text":{"content":"消息xxxxxxxxxxxxxxxxxxx内容"}}';
//$mms='{"msgtype":"text","text":{"content":"12消息内容"}}';
//$arrayobject = arrayToObject($mms);
//$req->setMsg($arrayobject);
$req->setMsg(($mms));

//$req->setMsg($mms);// 虽然报错 但是可以从发送消息
$resp=$c->execute($req, $accessToken,"https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");

///////////////////////////////////////////////////////////////////////更多消息格式
$mms1=array("msgtype"=>"text",
           "text"=>array("content"=>$user_info->name.',您好!您已通过钉钉扫码登陆WEB管理系统。  '.date("Y-m-d H:i:s")." &".rand(10000,99999))
          );

$mms2['msgtype']='link'; 
$mms2['link']['messageUrl']='https://work.ranjuan.cn';
$mms2['link']['picUrl']='@lALOACZwe2Rk';
$mms2['link']['title']=$user_info->name.' 您已登陆系统';
$mms2['link']['text']='点击查看详情~'.rand(1000,9999);


$mms['msgtype']= 'oa';
$mms["oa"]["message_url"]= "https://baike.baidu.com/item/%E5%BA%86%E7%A5%9D%E4%B8%AD%E5%8D%8E%E4%BA%BA%E6%B0%91%E5%85%B1%E5%92%8C%E5%9B%BD%E6%88%90%E7%AB%8B70%E5%91%A8%E5%B9%B4/23349273?fr=aladdin";
$mms["oa"]["head"]["bgcolor"]= "FFBBBBBB";
$mms["oa"]["head"]["text"]= "系统登录";/////向普通会话发送时有效,向企业会话发送时会被替换为微应用的名字),长度限制为最多10个字符
$mms["oa"]['body']['title']=date("Y-m-d H:i:s").'登录推送70周年';
$mms["oa"]['body']['form'][0]['key']='姓名:';
$mms["oa"]['body']['form'][0]['value']=$user_info->name;
$mms["oa"]['body']['form'][1]['key']='用户id:';
$mms["oa"]['body']['form'][1]['value']=$user_info->userid;
$mms["oa"]['body']['rich']['num']='抽奖码:'.rand(1111,8855);//富文本
$mms["oa"]['body']['rich']['unit']='NO.';
$mms["oa"]['body']['content']='正文简略信息,中华人民共和国成立70周年,庆祝中华人民共和国成立70周年大会10月1日举行,习近平将发表重要讲话。庆祝大会后,将举行盛大的庆祝中华人民共和国成立70周年阅兵式和群众游行。中共中央、国务院、中央军委将颁发“庆祝中华人民共和国成立70周年”纪念章。';
$mms["oa"]['body']['image']='@lADOADmaWMzazQKA';
$mms["oa"]['body']['file_count']='0';
$mms["oa"]['body']['author']='系统消息';


$mms4["msgtype"]="markdown";
$mms4["markdown"]["title"]="首屏会话透出的展示内容";
$mms4["markdown"]["text"]= "
# 这是支持markdown的文本 \n
## 标题2  \n
### 标题3333  \n
* 列表1 \n
* 列表12222 \n
**bold加粗显示** \n
*italic斜体显示* \n
> A 外事问谷歌,内事问度娘 \n
[this is a link](https://baidu.com) \n
![alt 啊](https://img.alicdn.com/tps/TB1XLjqNVXXXXc4XVXXXXXXXXXX-170-64.png)";


$mms5["msgtype"]= "action_card";
$mms5['action_card']['title']='店长/部门事务审批';
$mms5['action_card']['markdown']="### 标题3333  \n 周五 \n 部门三人 张三、赵四、王五 \n 一起去 南京奥体管 \n 参加荣誉企业颁奖典礼 \n [this is a link](https://baidu.com) \n";
$mms5['action_card']['btn_orientation']= "1";
$mms5['action_card']['btn_json_list'][0]['title']='拒绝';
$mms5['action_card']['btn_json_list'][0]['action_url']='https://www.baidu.com';
$mms5['action_card']['btn_json_list'][1]['title']='同意';
$mms5['action_card']['btn_json_list'][1]['action_url']='https://www.taobao.com';
最后编辑: admin  文档更新时间: 2021-03-16 21:50   作者:admin