search
HomeWeChat AppletWeChat DevelopmentRealize information collection of articles published on WeChat

Recently, a customer needed to implement such a function. After publishing article information on the WeChat public platform, they also needed to publish it again on the official website. This was equivalent to doing the same work twice. They wanted to implement it on the WeChat public platform. After the platform publishes the article, it can be published to the official website at the same time. Through research, the editor found that the official website of the WeChat public platform provides relevant information collection interfaces. The original address is as follows: https://developers.weixin.qq.com/doc /offiaccount/Asset_Management/Get_materials_list.html

The interface name is: Get permanent materials. In fact, when adding a new material article, click on the article material information saved after publishing, as shown below:

Realize information collection of articles published on WeChat

Only article materials that are being published can be collected if you click the publish button and save them to the publishing record--publishing, as shown below:

Realize information collection of articles published on WeChat

The editor will explain in detail below

Interface:

http request method:

POST https ://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=ACCESS_TOKEN

##1 Get ACCESS_TOKEN

public function getAccessToken(){
    $info = Db::name('access_token')->order('inputtime desc')->find();
    if($info){
        $time = time();
        $long_time = $time - $info['updatetime'];
        if($long_time>=$info['expires_in']){
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->secret;
            $res = $this->getJson($url);
            if(in_array('access_token',$this->DbSy->GetArrElement($res))){
                $insert['access_token'] = $res['access_token'];
                $insert['expires_in'] = $res['expires_in'];
                $insert['updatetime'] = time();
                $up_info = Db::name('access_token')->where('id',$info['id'])->update($insert);
                if(!$up_info){
                    return json(['code'=>103,'msg'=>'AccessToken更新失败']);
                }
                $AccessToken = $res['access_token'];
            }else{
                return json(['code'=>100,'msg'=>'AccessToken获取失败']);
            }
        }else{
            $AccessToken = $info['access_token'];
        }
    }else{
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->secret;
        $res = $this->getJson($url);
        if(in_array('access_token',$this->DbSy->GetArrElement($res))){
            $insert['access_token'] = $res['access_token'];
            $insert['expires_in'] = $res['expires_in'];
            $insert['updatetime'] = time();
            $insert['inputtime'] = time();
            $up_info = Db::name('access_token')->where('id',$info['id'])->insertGetId($insert);
            if($up_info){
                $AccessToken = $res['access_token'];
            }else{
                return json(['code'=>101,'msg'=>'AccessToken插入失败']);
            }

        }else{
            return json(['code'=>100,'msg'=>'AccessToken获取失败']);
        }
    }
    return $AccessToken;
}

Note:

Before production, you need to add the data table: access_token in the database, which is used to store the obtained access_token value, because the validity period of each obtained access_token value is 7200s, so as to avoid repeated acquisition of the access_token value.

2 Obtain the material list

After obtaining the access_token value, you can obtain the material list through the access_token value:

public function getArticleList($offset,$type='news',$length=20){
    $data = array(
        'type'=>$type,
        'offset'=>$offset,
        'count'=>$length
    );
    $json_data = json_encode($data);
    $AccessToken = $this->getAccessToken();
    $url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=".$AccessToken;
    $res = $this->getJsonData($url,$json_data);
    return $res;
    //dump($res);
}

Among them:

(1) type, the type of material, image, video, voice, news

(2) offset, the offset from all materials The position starts to return, 0 means returning from the first material

(3) count, the number of returned materials, the value is between 1 and 20

The above is the editor's summary of getting WeChat The public platform has published the method of article material information. Please forgive me if there are any shortcomings. I hope it can be helpful to all the masters.

The above is the detailed content of Realize information collection of articles published on 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version