Home  >  Article  >  Backend Development  >  PHP interface development tutorial: Implementing the external contact function of enterprise WeChat

PHP interface development tutorial: Implementing the external contact function of enterprise WeChat

WBOY
WBOYOriginal
2023-09-12 12:55:411678browse

PHP 接口开发教程:实现企业微信外部联系人功能

PHP Interface Development Tutorial: Implementing the External Contact Function of Enterprise WeChat

Introduction:
With the rapid popularity and development of Enterprise WeChat (WeCom), more and more More and more companies are beginning to use WeChat Enterprise to build communication channels with external contacts to better manage customers, suppliers and partners. This article will introduce how to use PHP development interface to implement the external contact function of Enterprise WeChat, including functions such as creating external contacts, obtaining external contact lists, and sending external contact messages.

1. Environment preparation:
To develop the external contact function of Enterprise WeChat, you first need to ensure the following environment preparation:

  1. Permissions for Enterprise WeChat application: In Enterprise WeChat In the background, create a self-built application and obtain the application ID and Secret for subsequent interface calls.
  2. PHP environment: Make sure your server has the PHP interpreter installed and the cURL module enabled.
  3. Development tools: You can choose any text editor, such as Sublime Text, PHPStorm, etc.

2. Obtain access_token:
Before making an interface call, we need to obtain an access_token as a credential for the interface call. The access_token has a certain validity period and needs to be obtained again after expiration.

Interface address:
https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ID&corpsecret=SECRET
(Note: Replace ID and SECRET with Enterprise WeChat ID and Secret of the created application)

The interface returns:
{

"errcode": 0,
"errmsg": "ok",
"access_token": "ACCESS_TOKEN",
"expires_in": 7200

}

Save the access_token locally for subsequent interface calls.

3. Create external contacts:
Now we can start to implement the function of creating external contacts. The following is an example of an interface call to create an external contact:

Interface address:
https://qyapi.weixin.qq.com/cgi-bin/externalcontact/add?access_token=ACCESS_TOKEN

Interface parameters:

Interface parameters are passed in JSON format, the example is as follows:
{

"external_contact": {
    "external_userid": "external_userid",
    "name": "contact_name",
    "nickname": "contact_nickname",
    "gender": 1,
    "position": "contact_position",
    "corp_name": "corporation_name",
    "type": 1
},
"follow_user": ["user_id_1", "user_id_2"]

}

Interface return:
{

"errcode": 0,
"errmsg": "ok",
"external_contact": {
    "external_userid": "external_userid"
}

}

In the above example, we passed the external contact details, including external contact ID, name, nickname, gender, position, company name and contact information, etc. At the same time, we can also designate follow-up personnel to let relevant enterprise WeChat users pay attention to the external contact.

4. Obtain the external contact list:
Next, let’s implement the function of obtaining the external contact list. The following is an example of an interface call to obtain an external contact list:

Interface address:
https://qyapi.weixin.qq.com/cgi-bin/externalcontact/list?access_token=ACCESS_TOKEN

Interface parameters:
{

"userid": "userid",
"cursor": "",
"limit": 100

}

Interface return:
{

"errcode": 0,
"errmsg": "ok",
"external_contact_list": [
    {
        "external_userid": "external_userid_1",
        "name": "contact_name_1",
        "position": "contact_position_1",
        "corp_name": "corporation_name_1",
        "type": 1,
        "follow_user": ["user_id_1"]
    },
    {
        "external_userid": "external_userid_2",
        "name": "contact_name_2",
        "position": "contact_position_2",
        "corp_name": "corporation_name_2",
        "type": 2,
        "follow_user": ["user_id_2", "user_id_3"]
    }
],
"next_cursor": "next_cursor"

}

In the above example, We passed the parameters for obtaining the external contact list, including user ID, cursor, and limit on the number returned each time. In the interface return, we can obtain the list information of external contacts, including the external contact's ID, name, position, company name, type, and follow-up personnel. If the total number of lists exceeds the number limit returned each time, you can use next_cursor for paging acquisition.

5. Send messages to external contacts:
Finally, let’s implement the function of sending messages to external contacts. The following is an example of an interface call for sending an external contact message:

Interface address:
https://qyapi.weixin.qq.com/cgi-bin/externalcontact/message/send?access_token=ACCESS_TOKEN

Interface parameters:
{

"touser": "user_id",
"msgtype": "text",
"text": {
    "content": "Hello, world!"
}

}

Interface return:
{

"errcode": 0,
"errmsg": "ok"

}

In the above example , we passed the parameters for sending the message, including the user ID that received the message and the message type. Here we used the text message type. In text message type we can pass the text content to be sent.

Conclusion:
Through this tutorial, we learned how to use PHP to develop interfaces to implement the external contact functions of Enterprise WeChat, including creating external contacts, obtaining external contact lists, and sending external contact messages, etc. Function. I hope this tutorial can be helpful to you, and you are welcome to try and expand it in practical applications. thanks for reading!

The above is the detailed content of PHP interface development tutorial: Implementing the external contact function of enterprise WeChat. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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