欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php企业微信获取员工userid以及打卡信息

程序员文章站 2023-11-19 16:29:10
企业微信可以通过部门列表获取部门下的员工信息,从而获取到员工的userid //首先获取需要的access_token $access_token = json_decode($this->getWork($url = 'https://qyapi.weixin.qq.com/cgi-bin/get ......

  企业微信可以通过部门列表获取部门下的员工信息,从而获取到员工的userid

  //首先获取需要的access_token

    $access_token = json_decode($this->getwork($url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=企业号的corpid&corpsecret=企业号的corpsecret'),true);

  //access_token获取后将它放置在缓存里,不能频繁调用gettoken接口,否则会受到频率拦截 access_token有效期是2个小时
    cache::set('access_token',$access_token['access_token'],7200);

  public function getwork($url){
    $curl = curl_init();
    curl_setopt($curl, curlopt_url, $url);
    curl_setopt($curl, curlopt_ssl_verifypeer, false);
    curl_setopt($curl, curlopt_ssl_verifyhost, false);
    curl_setopt($curl, curlopt_returntransfer, 1);
    //curl_setopt($ch, curlopt_httpheader, $headers );
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
  }

 

  //获取到access_token之后可以获取当前企业下的部门列表

  $department = $this->getwork($url='https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token='.cache::get('access_token'));

  //获取部门下的员工信息(userid)

  $simplelist = json_decode($this->getwork($url='https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token='.cache::get('access_token').'&department_id=18'),true);

  //获取到员工的userid后可以进行当前员工的打卡记录查询

    $stardate = date('y-m-01');//当前月份第一天
    $enddate = date('y-m-d');//当前月份
    $url = 'https://qyapi.weixin.qq.com/cgi-bin/checkin/getcheckindata?access_token='.cache::get('access_token');
    $post['opencheckindatatype']=3;
    $post['starttime']= strtotime($stardate);
    $post['endtime']= strtotime($enddate);
    $post['useridlist']=$simplelist['userlist'][3]['userid'];////员工的userid
    $res = $this->request_post($url,json_encode($post));
    var_dump($res);