Practical steps to implement working hours statistics through enterprise WeChat interface and PHP

WBOY
Release: 2023-07-06 10:50:02
Original
797 people have browsed it

Practical steps for realizing working hours statistics through Enterprise WeChat interface and PHP

Enterprise WeChat is an office communication tool that provides a rich interface to integrate with other systems. Within the enterprise, working time statistics is an important management requirement. Through the combination of the enterprise WeChat interface and PHP, the working time statistics function can be easily realized. This article will introduce the practical steps for implementing working hours statistics through the enterprise WeChat interface and PHP, and provide corresponding code examples.

Step 1: Preparation

First, you need to have an enterprise WeChat account and create an application. In the enterprise WeChat background management interface, click "Application Management"->"Create Application", fill in the corresponding information and save it. After successful creation, you can obtain important information such as CorpID, Secret, and AgentID.

Secondly, you need a server to set up a PHP environment, such as Apache or Nginx, and install PHP's curl extension.

Step 2: Obtain access_token

Access_token is the credential used by the enterprise WeChat interface. The access_token obtained by each application is independent. We can obtain access_token through the interface provided by Enterprise WeChat.

Please note that the access_token is valid for 2 hours and needs to be refreshed regularly. We can use server scheduled tasks (such as cronjob) to obtain new access_token regularly.

The following is an example of PHP code to obtain access_token:

<?php
$corpId = "your_corpId";
$secret = "your_secret";

$url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={$corpId}&corpsecret={$secret}";

$response = httpGet($url);
$result = json_decode($response);

if ($result->errcode == 0) {
    $accessToken = $result->access_token;
    echo "Access token: {$accessToken}";
} else {
    echo "Failed to get access token: {$result->errmsg}";
}

function httpGet($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);
    return $response;
}
?>
Copy after login

Step 3: Use the interface to obtain member information

In working hours statistics, you need to obtain member information in corporate WeChat. In order to calculate the working hours of each member. We can use the interface provided by Enterprise WeChat to obtain member information.

The following is an example of PHP code to obtain member information:

<?php
$accessToken = "your_accessToken";

$userId = "your_userId";

$url = "https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token={$accessToken}&userid={$userId}";

$response = httpGet($url);
$result = json_decode($response);

if ($result->errcode == 0) {
    $name = $result->name;
    echo "Member name: {$name}";
} else {
    echo "Failed to get member info: {$result->errmsg}";
}
?>
Copy after login

Step 4: Use the interface to punch in the record

Enterprise WeChat provides an interface to obtain the punch in record, we can use these Records are used to calculate the hours worked by each member.

The following is an example of PHP code to obtain punch-in records:

<?php
$accessToken = "your_accessToken";

$userId = "your_userId";
$fromDate = "2022-01-01";
$toDate = "2022-01-31";

$url = "https://qyapi.weixin.qq.com/cgi-bin/checkin/getcheckindata?access_token={$accessToken}";

$data = [
    "opencheckindatatype" => 3,
    "starttime" => strtotime($fromDate),
    "endtime" => strtotime($toDate),
    "useridlist" => [$userId]
];

$response = httpPost($url, json_encode($data));
$result = json_decode($response);

if ($result->errcode == 0) {
    foreach ($result->checkindata as $checkin) {
        $time = date("Y-m-d H:i:s", $checkin->checkin_time);
        echo "Check-in time: {$time}
";
    }
} else {
    echo "Failed to get check-in data: {$result->errmsg}";
}

function httpPost($url, $data)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $response = curl_exec($ch);
    curl_close($ch);
    return $response;
}
?>
Copy after login

Step 5: Statistics of working hours

Use the obtained punch-in records to count working hours. According to actual needs, each member's working hours can be calculated on a daily, weekly or monthly basis.

The following is an example of PHP code for counting working hours:

<?php
$checkinData = [
    // 打卡记录数组
    // ...
];

$workTime = 0;

for ($i = 0; $i < count($checkinData) - 1; $i += 2) {
    $checkinTime = $checkinData[$i];
    $checkoutTime = $checkinData[$i + 1];

    $workTime += strtotime($checkoutTime) - strtotime($checkinTime);
}

$workHours = $workTime / 3600;

echo "Total work hours: {$workHours} hours";
?>
Copy after login

The above are the practical steps for using the enterprise WeChat interface and PHP to implement working hour statistics. Through the above steps, you can easily obtain member information, punch-in records, and count working hours. We can make corresponding adjustments according to actual needs to meet different working hour statistical needs. Hope this article is helpful to everyone!

The above is the detailed content of Practical steps to implement working hours statistics through enterprise WeChat interface and PHP. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!