How to Retrieve Published Articles from the WeChat Public Platform Using PHP
This article explains how to use the WeChat public platform's material management API to automatically retrieve published article content and sync it to a website, covering access token acquisition, API request details, PHP code examples, required database setup, and parameter explanations.
A client needs to avoid duplicating work by publishing an article on the WeChat public platform and then automatically publishing the same content on their own website. The solution leverages WeChat's material management API, which provides access to permanent article materials.
The relevant API endpoint is a POST request to
https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=ACCESS_TOKEN. This endpoint returns a list of stored materials when supplied with a valid access token.
First, obtain a valid access_token. The following PHP function retrieves the token, stores it in a database table named access_token, and refreshes it when it expires (7200 seconds):
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;
}Before using this function, create a database table named access_token to store the token and its expiration time, preventing unnecessary repeated requests.
With a valid token, retrieve the list of article materials using the following PHP method:
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);
}The parameters are:
type : material type – image, video, voice, or news (article).
offset : starting position in the material list; 0 returns from the first item.
count : number of items to return, ranging from 1 to 20.
By following these steps, developers can automatically pull already published WeChat articles and push them to their own website, streamlining content management across platforms.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
