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

  企业微信可以通过部门列表获取部门下的员工信息,从而获取到员工的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);

 

你可能感兴趣的:(php企业微信获取员工userid以及打卡信息)