PHP获取钉钉考勤信息源代码

    <?php
    /*
    https://open-doc.dingtalk.com/microapp/serverapi2/oek45u 官方开发文档
    PHP版本钉钉考勤获取
    Author:RainMan
    交流QQ:9359951
    corpid 和 corpsecret 在 https://open-dev.dingtalk.com/#/devAuthorize 开发授权中获取
    */
    function curl_http_request($href, $method = 0, $post_data = null, $cookie = null) {
    if (isset($post_data) && is_string($post_data) && strlen($post_data) > 0) $post_fields = $post_data;
    else if (isset($post_data) && is_array($post_data) && count($post_data) > 0) $post_fields = http_build_query($post_data, null, '&', PHP_QUERY_RFC3986);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $href);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POST, $method);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
    if (isset($post_fields)) curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
    if (isset($cookie)) curl_setopt($ch, CURLOPT_COOKIE, $cookie);
    $result = curl_exec($ch);
    $curl_info = curl_getinfo($ch);
    $info = $curl_info['url'] . '|' . $curl_info['http_code'] . '|' . $curl_info['total_time'];
    $header_size = $curl_info['header_size'];
    $header = substr($result, 0, $header_size);
    $body = substr($result, $header_size);
    $success = true;
    $uri = $_SERVER["REQUEST_URI"];
    if ($curl_info['http_code'] == 0 || $curl_info['http_code'] >= 400) {
    $success = false;
    }
    curl_close($ch);
    if ($success) {
    return $body;
    }
    return false;
    }
    //echo "<br><br><br><center><font color=green>输出测试,请耐心等候......</font></center>";
    $urltoken = "https://oapi.dingtalk.com/gettoken?corpid=开发授权中获取&corpsecret=开发授权中获取";
    //echo curl_http_request($urltoken)."<br>";
    $access_token = json_decode(curl_http_request($urltoken), true); //获取 access_token
    $access_token = $access_token[access_token]; //获取 access_token

    $url="https://oapi.dingtalk.com/attendance/listRecord?access_token=".$access_token;

    //初始化
    $ch = curl_init();
    //设置抓取的url
    curl_setopt($ch, CURLOPT_URL, $url);

    //开启header
    curl_setopt($ch, CURLOPT_HEADER, 0);
    //类型为json
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8'));

    //设置获取的信息以文件流的形式返回,而不是直接输出。
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    //设置post方式提交
    curl_setopt($ch, CURLOPT_POST, 1);
    //设置post数据
    $post_data = '{"userIds":["manager9202"],"checkDateFrom":"2018-11-20 00:00:00","checkDateTo":"2018-11-26 23:59:59","isI18n":"false"}';

    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    //执行命令
    $output = curl_exec($ch);
    //关闭URL请求
    curl_close($ch);
    //显示获得的数据
    echo $output."<br><br>";

    $out_array = json_decode($output,true);

    if($out_array[errcode]==0){
    foreach ($out_array["recordresult"] as $DPID){
    echo $DPID[userId]." - ".$DPID[checkType]." - ".date("Y-m-d H:i:s",substr($DPID[workDate],0,-3))." - ".date("Y-m-d H:i:s",substr($DPID[userCheckTime],0,-3))."<br>";
    }
    }else{
    echo "<br><br><center><font color=red>参数错误 $out_array[errcode] $out_array[errmsg] </font></center>";
    }

    ?>

    转载请注明:RAIN MAN » PHP获取钉钉考勤信息源代码

    喜欢 9

还没有人抢沙发呢~