PHP enterprise wechat obtains employee userid and clock in information

Time:2020-1-13

Enterprise wechat can obtain the employee information under the Department through the Department list, so as to obtain the employee’s userid

//First obtain the required access_token

$access_token = json_decode ($this – > Getwork ($url =’https://qyapi.weixin.qq.com/cgi-bin/gettoken? Corpid = Corpid of enterprise number & corpsecret = corpsecret of enterprise number), true);

/ / after access_token is obtained, it will be placed in the cache, and the gettoken interface cannot be called frequently, otherwise it will be blocked by frequency. The validity period of access_token is 2 hours
    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;
  }

 

//After access_token is obtained, you can get the Department list under the current enterprise

  $department = $this->getWork($url=’https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=’.Cache::get(‘access_token’));

//Get employee information under the Department (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);

//After obtaining the userid of the employee, you can query the clock in record of the current employee

$stardate = date (‘y-m-01 ‘); / / the first day of the current month
$enddate = date (‘y-m-d ‘); / / current month
    $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’] [‘userid ‘]; / / / employee’s userid
    $res = $this->request_post($url,json_encode($post));
    var_dump($res);