Home  >  Article  >  WeChat Applet  >  Realize information collection of articles published on WeChat

Realize information collection of articles published on WeChat

XuPing.Yang
XuPing.YangOriginal
2022-02-12 13:13:153805browse

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