php获取钉钉所有人员信息

1.先觉条件

// 开放对应接口权限
$appkey  = 'xxxxxx'; //微应用的appKey
$appsecret = 'xxxxxx'; //微应用的appSecret
$departmentUrl = 'https://oapi.dingtalk.com/topapi/v2/department/listsub'; //获取部门url
$userInfoUrl = 'https://oapi.dingtalk.com/topapi/v2/user/list'; //获取人员信息url
$companyID =11111; //公司id

2.获取accessToken

/**
 * 获取钉钉AccessToken
 *
 * @access public
 * @return void
 * */
function getDindinAccessToken()
{
    $appkey = 'appkey'; //微应用的appKey
    $appsecret = 'appsecret'; //微应用的appSecret

    //定义token文件和路径
    $token_file_name = __DIR__ . "/ding_access_token.php"; //存的目录自己定义
    if (file_exists($token_file_name)) {
        $token_file_content = json_decode(get_php_file($token_file_name));
        //token超时
        if ($token_file_content->expire_time < time()) {
            //从接口获取token
            $token = getDindinToken($appkey, $appsecret, $token_file_name);
        } else {
            //否则使用缓存的token
            $token = $token_file_content->access_token;
        }
    } else {
        //接口获取token
        $token = getDindinToken($appkey, $appsecret, $token_file_name);
    }

    return $token;
}

/**
 * 获取钉钉AccessToken
 *
 * @param string  $appkey
 * @param string  $appsecret
 * @param string  $token_file_name
 * @access public
 * @return void
 * */
function getDindinToken($appkey, $appsecret, $token_file_name)
{
    $url = "https://oapi.dingtalk.com/gettoken?appkey=" . $appkey . "&appsecret=" . $appsecret;
    $get_result = json_decode(http($url), true);
    if ($get_result['errcode'] == '0') {
        $new_token = (object)[];
        $token = $get_result['access_token'];
        $new_token->expire_time = time() + 7000;
        $new_token->access_token = $token;
        set_php_file($token_file_name, json_encode($new_token));
    } else {
        $token = 'get_token_error';
    }

    return     $token;
}

function get_php_file($filename)
{
    return trim(substr(file_get_contents($filename), 15));
}

function set_php_file($filename, $content)
{
    $fp = fopen($filename, "w");
    fwrite($fp, "<?php exit();?>" . $content);
    return fclose($fp);
}

function http($url, $data = null)
{
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    if (!empty($data)) {
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($curl, CURLOPT_URL, $url);

    $res = curl_exec($curl);
    curl_close($curl);

    return $res;
}

3.获取部门的ID (可以循环获取有多少部门循环多少次)

/**
 * 获取下级部门dept_id
 */
function getTeamInfo()
{
    $url = $departmentUrl . "?access_token=" . getDindinAccessToken();
    $data = [
        'dept_id' => $companyID, // 公司ID
    ];
    $teamRes = http($url, $data);
    $teamData = json_decode($teamRes);
    $teamArr = [];

    foreach ($teamData->result as $teamName) {
        $teamArr[] = $teamName;
    }
    return $teamArr;
}

4.获取单个部门人员信息

/**
 * 获取单个部门下人员信息
 */
function arrk($dept_id)
{
    $url = $userInfoUrl . "?access_token=" . getDindinAccessToken();
    $userArr = [];
    $cursor = 0;
    do {
        $data = [
            'dept_id' => $dept_id,
            'cursor' => $cursor,
            'size' => 100,
        ];
        $userRes = http($url, $data);
        $userData = json_decode($userRes);
        $userInfoArr = @$userData->result->list;
        $count = @count($userInfoArr);
        if ($count == 0) {
            return $userArr;
        }
        $cursor = @$userData->result->next_cursor;
    } while ($count >= 1);
    return $userArr;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值