Home  >  Article  >  Backend Development  >  Implementing WeChat public account material library classification management in PHP

Implementing WeChat public account material library classification management in PHP

WBOY
WBOYOriginal
2023-05-13 17:00:341433browse

With the popularity and use of WeChat official accounts, more and more companies and individuals have begun to use WeChat official accounts for publicity and promotion. The material library in the WeChat public account is one of the important tools for companies and individuals to publicize and promote. However, there are many kinds of materials in the material library, which makes it difficult to manage. This article will introduce how to implement classification management of WeChat public account material library in PHP.

1. WeChat public platform material management interface

The material management in the WeChat public account is divided into two types: permanent materials and temporary materials. Permanent material means that after it is uploaded to the WeChat server, it is permanently stored there and will not be deleted or replaced. Temporary material means that after uploading to the WeChat server, it will only be saved for three days and will be deleted after expiration. In the WeChat public platform, the material management interface includes the following five interfaces:

  1. Upload picture and text material interface (uploadnews)
  2. Upload picture material interface (uploadimg)
  3. Upload voice material interface (uploadvoice)
  4. Upload video material interface (uploadvideo)
  5. Upload thumbnail material interface (uploadthumb)

Through these interfaces, users Various types of materials can be uploaded on the WeChat public platform.

2. WeChat public platform material library classification management

  1. Create material library classification

First, we need to create a material library classification in the WeChat public platform . You can enter the material library interface through the material management menu in the official account settings, and then click the " " button in the category label at the top to create a new category.

  1. Get the material library classification list

In PHP, we can get the material library classification list by calling the interface provided by the WeChat public platform. The specific steps are as follows:

(1) Call the access_token interface provided by the WeChat public platform to obtain the access_token. access_token is used to authenticate the interface.

(2) Call the material library classification management interface provided by WeChat public platform and obtain the material library classification list through GET method. The code example is as follows:

$url = "https://api.weixin.qq.com/cgi-bin/material/get_materialcount?access_token=".$access_token;
$result = file_get_contents($url);
  1. Add materials to the corresponding category

When we need to add materials, we can upload them by calling the relevant interface in the WeChat public platform material management interface Materials and upload them to the corresponding categories. The specific code example is as follows:

$url = "https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=".$access_token;
$data = array(
    "articles" => array(
        array(
            "title" => "标题",
            "thumb_media_id" => "图片素材ID",
            "author" => "作者",
            "digest" => "摘要",
            "show_cover_pic" => 1,
            "content" => "内容",
            "content_source_url" => "原文链接"
        )
    )
);
$data_string = json_encode($data, JSON_UNESCAPED_UNICODE);
$result = https_request($url, $data_string);

It should be noted that when uploading graphics and text materials, you need to upload the cover image of the graphics and text to the WeChat server first, and obtain the media_id of the material. For how to upload picture materials, see the "Upload Picture Materials Interface" above.

  1. Get the material list under the corresponding category

Users can obtain the material list under the corresponding category by calling the "Get permanent material list" interface in the WeChat public platform material management interface. Material list. Specific code examples are as follows:

$url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=".$access_token;
$data = array(
    "type" => "image",
    "offset" => 0,
    "count" => 20
);
$data_string = json_encode($data, JSON_UNESCAPED_UNICODE);
$result = https_request($url, $data_string);

It should be noted that when obtaining the material list, you need to specify the material type (i.e. "type" parameter), as well as the starting position and number of paging (i.e. "offset" and "count" parameter).

3. Summary

This article introduces how to implement WeChat public account material library classification management in PHP. By calling the interface provided by the WeChat public platform, users can implement classification management of the material library, upload and obtain materials in their own public accounts, thus making publicity and promotion more convenient.

The above is the detailed content of Implementing WeChat public account material library classification management in PHP. 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