PHP微信公众号素材管理

获取access_token可参考上篇文章 PHP微信公众号开发(一)

本文讲解公众号图文和图片素材的获取

获取素材列表接口文档

接口的地址及参数

1
2
3
4
5
6
7
8
9
10
11
12
13
http请求方式: POST
https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=ACCESS_TOKEN

{
"type":TYPE,
"offset":OFFSET,
"count":COUNT
}

参数 是否必须 说明
type 是 素材的类型,图片(image)、视频(video)、语音 (voice)、图文(news)
offset 是 从全部素材的该偏移位置开始返回,0表示从第一个素材 返回
count 是 返回素材的数量,取值在1到20之间

offset为获取列表的偏移量,count为获取数量(最多每页取20条数据)。因此,我们想要获取全部素材的时候可以获取总素材数量,计算出总页数,循环得到每页偏移量进行分页获取。

获取素材总数接口文档

接口的地址及参数

1
2
3
4
5
6
7
8
9
10
http请求方式: GET
https://api.weixin.qq.com/cgi-bin/material/get_materialcount?access_token=ACCESS_TOKEN

//返回说明
{
"voice_count":COUNT, 语音总数量
"video_count":COUNT, 视频总数量
"image_count":COUNT, 图片总数量
"news_count":COUNT 图文总数量
}

代码示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* 获取素材列表
* @param integer $is_get_all [是否获取全部素材]
* @param integer $offset [全部素材的偏移位置,0表示从第一个素材]
* @param integer $count [返回素材的数量,取值在1到20之间]
* @param string $type [素材的类型,图片(image)、视频(video)、语音 (voice)、图文(news)]
* @return [type] [description]
*/
public function actionBatchMaterial($is_get_all = 0,$offset = 0,$count = 20,$type = 'news'){

$options = array(
'appid'=>$appid, //公众号AppID
'appsecret'=>$appsecret, //公众号AppSecret
);
$this->wechat = new Wechat($options); //此类文件可从参考链接(微信公众平台php sdk)获取

$list_count = 0; //素材数量
if ($is_get_all) { //获取全部素材
$material_count = $this->getForeverCount(); //获取素材总数
if ($material_count) {
switch ($type) {
case 'news':
$list_count = $material_count['news_count'];
break;
case 'image':
$list_count = $material_count['image_count'];
break;
case 'video':
$list_count = $material_count['video_count'];
break;
case 'voice':
$list_count = $material_count['voice_count'];
break;
default:
echo "素材类型错误".PHP_EOL;
exit();
break;
}
}
}

$material_list = [];

if ($is_get_all && $list_count) {
$page = ceil($list_count/$count);//计算总页数
for ($i=0; $i < $page; $i++) { //分页获取素材数据
$foreverList = $this->getForeverList($type,$offset,$count);
if ($foreverList) {
$material_list[] = $foreverList['item'];
}
$offset += $count;//计算下页偏移量
}
}else{ //默认同步最新一页数据
$foreverList = $this->getForeverList($type,$offset,$count);
if ($foreverList) {
$material_list[] = $foreverList['item'];
}
}

}


/**
* [获取图文素材总数]
{
"voice_count":COUNT,
"video_count":COUNT,
"image_count":COUNT,
"news_count":COUNT
}
* @return [type] [description]
*/
private function getForeverCount(){
$count = $this->wechat->getForeverCount();
if (!empty($this->wechat->errMsg) ) {
exit($this->wechat->errMsg);
}

return $count;
}

/**
* 获取素材列表
* @param string $type [素材的类型,图片(image)、视频(video)、语音 (voice)、图文(news)]
* @param integer $offset [全部素材的偏移位置,0表示从第一个素材]
* @param integer $count [返回素材的数量,取值在1到20之间]
* @return [type] [description]
*/
private function getForeverList($type,$offset,$count){
$list = $this->wechat->getForeverList($type,$offset,$count);
if (!empty($this->wechat->errMsg) ) {
exit($this->wechat->errMsg);
}

return $list;
}

[参考链接]

微信公众平台php sdk

踏浪 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持将鼓励我继续创作!