Home > Backend Development > PHP Tutorial > Sharing tips on reimbursement application for connecting enterprise WeChat interface with PHP

Sharing tips on reimbursement application for connecting enterprise WeChat interface with PHP

WBOY
Release: 2023-07-06 18:22:01
Original
1318 people have browsed it

Sharing of reimbursement application skills for connecting the enterprise WeChat interface with PHP

With the continuous development of enterprise information construction, internal communication, management and coordination of enterprises also urgently need to rely on modern communication tools. As an enterprise-level application software that integrates real-time communication, collaboration, and office work, Enterprise WeChat has become the first choice for more and more enterprises.

Enterprise WeChat provides a rich interface to facilitate enterprises to integrate with third-party systems to achieve customized business needs. Among them, reimbursement application is one of the common workflows in enterprises. This article will share some tips on enterprise WeChat interface docking and PHP-based reimbursement application to help everyone get started quickly.

1. Enterprise WeChat interface docking

To realize the connection between Enterprise WeChat and your own system, you first need to carry out corresponding development and configuration.

  1. Register Enterprise WeChat

Register on the official website of Enterprise WeChat and obtain important information such as enterprise ID, application ID, and application secret.

  1. Create a self-built application

Create a self-built application in the enterprise WeChat backend, set the application name, application logo, etc., and select the required permission range.

  1. Get access_token

During the development process, access_token is needed to verify the interface access permissions. Access_token can be obtained through the API interface provided by Enterprise WeChat. The specific code is as follows:

<?php

// 获取access_token
function getAccessToken($corpid, $corpsecret) {
    $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={$corpid}&corpsecret={$corpsecret}";
    $result = httpGet($url);
    $access_token = json_decode($result, true)["access_token"];
    return $access_token;
}

// 发送HTTP GET请求
function httpGet($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_URL, $url);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

// 示例调用
$corpid = "YourCorpID";
$corpsecret = "YourCorpSecret";
$access_token = getAccessToken($corpid, $corpsecret);
echo $access_token;

?>
Copy after login

Through the above code, access_token can be obtained to prepare for permission verification for interface calls.

  1. Call the Enterprise WeChat interface

Next, you can call the interface provided by Enterprise WeChat to implement the corresponding functions according to your business needs. For example, to send application messages, the code is as follows:

<?php

// 发送应用消息
function sendAppMessage($access_token, $agentid, $users, $content) {
    $url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={$access_token}";
    $data = array(
        "touser" => $users,
        "agentid" => $agentid,
        "msgtype" => "text",
        "text" => array(
            "content" => $content
        ),
        "safe" => 0
    );
    $result = httpPost($url, json_encode($data, JSON_UNESCAPED_UNICODE));
    return $result;
}

// 发送HTTP POST请求
function httpPost($url, $data) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

// 示例调用
$access_token = "YourAccessToken";
$agentid = "YourAgentID";
$users = "UserID1|UserID2";
$content = "Hello, World!";
$result = sendAppMessage($access_token, $agentid, $users, $content);
echo $result;

?>
Copy after login

Through the above code, the function of sending application messages to specified users can be realized.

2. Example of PHP reimbursement application

In actual work, reimbursement application is a common process. The following uses PHP as an example to demonstrate an example of reimbursement application based on the enterprise WeChat interface.

<?php

// 提交报销申请
function submitExpenseClaim($access_token, $userid, $expenses) {
    $url = "https://qyapi.weixin.qq.com/cgi-bin/oa/applyevent?access_token={$access_token}";
    $data = array(
        "userid" => $userid,
        "apply_data" => array(
            "contents" => array(
                array(
                    "title" => "报销明细",
                    "fields" => array(
                        array(
                            "type" => "text",
                            "title" => "费用类型",
                            "value" => "餐饮费"
                        ),
                        array(
                            "type" => "money",
                            "title" => "金额",
                            "value" => 100
                        )
                    )
                )
            )
        )
    );
    $result = httpPost($url, json_encode($data, JSON_UNESCAPED_UNICODE));
    return $result;
}

// 示例调用
$access_token = "YourAccessToken";
$userid = "UserID";
$expenses = array(
    array(
        "费用类型" => "餐饮费",
        "金额" => 100
    ),
    array(
        "费用类型" => "交通费",
        "金额" => 200
    )
);
$result = submitExpenseClaim($access_token, $userid, $expenses);
echo $result;

?>
Copy after login

Through the above code, you can implement the function of submitting reimbursement applications to Enterprise WeChat and obtain the corresponding return results.

Summary:

This article briefly introduces the basic steps of enterprise WeChat interface docking, and provides some sample codes to help you understand how to use PHP to perform reimbursement application-related operations with Enterprise WeChat. Of course, the enterprise WeChat interface has many other powerful functions. I hope this article can inspire everyone and be useful in actual work.

The above is the detailed content of Sharing tips on reimbursement application for connecting enterprise WeChat interface with 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