瀏覽代碼

公众号类 更新&增加临时/永久素材接口:
1. 多媒体文件接口 更新为 临时素材接口(新接口更为规范)
2. 增加 永久素材系列接口(上传/获取/删除等)

由于官方文档有坑,关于永久素材接口中视频的上传获取需要注意是否正常

binsee 10 年之前
父節點
當前提交
ec900997af
共有 3 個文件被更改,包括 240 次插入17 次删除
  1. 10 3
      README.md
  2. 221 12
      wechat.class.php
  3. 9 2
      wiki/官方API类库.md

+ 10 - 3
README.md

@@ -80,7 +80,7 @@ https://mp.weixin.qq.com/cgi-bin/readtemplate?t=business/course2_tmpl&lang=zh_CN
 * getRevTo()  返回消息接收者的id(即公众号id)
 * getRevType() 返回接收消息的类型
 * getRevID() 返回消息id
-* getRevCtime() 返回消息发送事件
+* getRevCtime() 返回消息发送时间
 * getRevContent() 返回消息内容正文或语音识别结果(文本型)
 * getRevPic() 返回图片信息(图片型信息) 返回数组{'mediaid'=>'','picurl'=>''}
 * getRevLink() 接收消息链接(链接型信息) 返回数组{'url'=>'','title'=>'','description'=>''}
@@ -161,8 +161,15 @@ const EVENT_CARD_USER_DEL = 'user_del_card';        //卡券 - 用户删除卡
  *  getServerIp() 获取微信服务器IP地址列表 返回数组array('127.0.0.1','127.0.0.1')
  *  getMenu() 获取菜单 
  *  deleteMenu() 删除菜单 
- *  uploadMedia($data, $type) 上传多媒体文件(注意上传大文件时可能需要先调用 set_time_limit(0) 避免超时)
- *  getMedia() 获取接收到的音频、视频媒体文件 
+ *  uploadMedia($data, $type) 上传临时素材,有效期为3天(注意上传大文件时可能需要先调用 set_time_limit(0) 避免超时)
+ *  getMedia($media_id,$is_video=false) 获取临时素材(含接收到的音频、视频媒体文件)
+ *  uploadForeverMedia($data, $type,$is_video=false,$video_info=array()) 上传永久素材,可以在公众平台官网素材管理模块中看到
+ *  uploadForeverArticles($data) 上传永久图文素材
+ *  updateForeverArticles($media_id,$data,$index=0) 修改永久图文素材(认证后的订阅号可用)
+ *  getForeverMedia($media_id,$is_video=false) 获取永久素材
+ *  delForeverMedia($media_id) 删除永久素材
+ *  getForeverList($type,$offset,$count) 获取永久素材列表(认证后的订阅号可用)
+ *  getForeverCount() 获取永久素材总数
  *  uploadMpVideo($data) 上传视频素材,当需要群发视频时,必须使用此方法得到的MediaID,否则无法显示
  *  uploadArticles($data) 上传图文消息素材
  *  sendMassMessage($data) 高级群发消息

+ 221 - 12
wechat.class.php

@@ -108,6 +108,13 @@ class Wechat
 	const MEDIA_UPLOAD_URL = '/media/upload?';
 	const MEDIA_GET_URL = '/media/get?';
 	const MEDIA_VIDEO_UPLOAD = '/media/uploadvideo?';
+    const MEDIA_FOREVER_UPLOAD_URL = '/material/add_material?';
+    const MEDIA_FOREVER_NEWS_UPLOAD_URL = '/material/add_news?';
+    const MEDIA_FOREVER_NEWS_UPDATE_URL = '/material/update_news?';
+    const MEDIA_FOREVER_GET_URL = '/material/get_material?';
+    const MEDIA_FOREVER_DEL_URL = '/material/del_material?';
+    const MEDIA_FOREVER_COUNT_URL = '/material/get_materialcount?';
+    const MEDIA_FOREVER_BATCHGET_URL = '/material/batchget_material?';
 	const OAUTH_PREFIX = 'https://open.weixin.qq.com/connect/oauth2';
 	const OAUTH_AUTHORIZE_URL = '/authorize?';
 	///多客服相关地址
@@ -1483,16 +1490,18 @@ class Wechat
 	}
 
 	/**
-	 * 上传多媒体文件(认证后的订阅号可用)
+	 * 上传临时素材,有效期为3天(认证后的订阅号可用)
 	 * 注意:上传大文件时可能需要先调用 set_time_limit(0) 避免超时
 	 * 注意:数组的键值任意,但文件名前必须加@,使用单引号以避免本地路径斜杠被转义
+     * 注意:临时素材的media_id是可复用的!
 	 * @param array $data {"media":'@Path\filename.jpg'}
 	 * @param type 类型:图片:image 语音:voice 视频:video 缩略图:thumb
 	 * @return boolean|array
 	 */
 	public function uploadMedia($data, $type){
 		if (!$this->access_token && !$this->checkAuth()) return false;
-		$result = $this->http_post(self::UPLOAD_MEDIA_URL.self::MEDIA_UPLOAD_URL.'access_token='.$this->access_token.'&type='.$type,$data,true);
+		//原先的上传多媒体文件接口使用 self::UPLOAD_MEDIA_URL 前缀
+		$result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_UPLOAD_URL.'access_token='.$this->access_token.'&type='.$type,$data,true);
 		if ($result)
 		{
 			$json = json_decode($result,true);
@@ -1507,28 +1516,228 @@ class Wechat
 	}
 
 	/**
-	 * 根据媒体文件ID获取媒体文件(认证后的订阅号可用)
+	 * 获取临时素材(认证后的订阅号可用)
 	 * @param string $media_id 媒体文件id
+	 * @param boolean $is_video 是否为视频文件,默认为否
 	 * @return raw data
 	 */
-	public function getMedia($media_id){
+	public function getMedia($media_id,$is_video=false){
 		if (!$this->access_token && !$this->checkAuth()) return false;
-		$result = $this->http_get(self::UPLOAD_MEDIA_URL.self::MEDIA_GET_URL.'access_token='.$this->access_token.'&media_id='.$media_id);
+		//原先的上传多媒体文件接口使用 self::UPLOAD_MEDIA_URL 前缀
+		//如果要获取的素材是视频文件时,不能使用https协议,必须更换成http协议
+		$url_prefix = $is_video?str_replace('https','http',self::API_URL_PREFIX):self::API_URL_PREFIX;
+		$result = $this->http_get($url_prefix.self::MEDIA_GET_URL.'access_token='.$this->access_token.'&media_id='.$media_id);
 		if ($result)
 		{
-			$json = json_decode($result,true);
-			if (isset($json['errcode'])) {
-				$this->errCode = $json['errcode'];
-				$this->errMsg = $json['errmsg'];
-				return false;
-			}
+            if (is_string($result)) {
+                $json = json_decode($result,true);
+                if (isset($json['errcode'])) {
+                    $this->errCode = $json['errcode'];
+                    $this->errMsg = $json['errmsg'];
+                    return false;
+                }
+            }
 			return $result;
 		}
 		return false;
 	}
 
+
+    /**
+     * 上传永久素材(认证后的订阅号可用)
+     * 新增的永久素材也可以在公众平台官网素材管理模块中看到
+     * 注意:上传大文件时可能需要先调用 set_time_limit(0) 避免超时
+     * 注意:数组的键值任意,但文件名前必须加@,使用单引号以避免本地路径斜杠被转义
+     * @param array $data {"media":'@Path\filename.jpg'}
+     * @param type 类型:图片:image 语音:voice 视频:video 缩略图:thumb
+     * @param boolean $is_video 是否为视频文件,默认为否
+     * @param array $video_info 视频信息数组,非视频素材不需要提供 array('title'=>'视频标题','introduction'=>'描述')
+     * @return boolean|array
+     */
+    public function uploadForeverMedia($data, $type,$is_video=false,$video_info=array()){
+        if (!$this->access_token && !$this->checkAuth()) return false;
+        //#TODO 暂不确定此接口是否需要让视频文件走http协议
+        //如果要获取的素材是视频文件时,不能使用https协议,必须更换成http协议
+        //$url_prefix = $is_video?str_replace('https','http',self::API_URL_PREFIX):self::API_URL_PREFIX;
+        //当上传视频文件时,附加视频文件信息
+        if ($is_video) $data['description'] = self::json_encode($video_info);
+        $result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_FOREVER_UPLOAD_URL.'access_token='.$this->access_token.'&type='.$type,$data,true);
+        if ($result)
+        {
+            $json = json_decode($result,true);
+            if (!$json || !empty($json['errcode'])) {
+                $this->errCode = $json['errcode'];
+                $this->errMsg = $json['errmsg'];
+                return false;
+            }
+            return $json;
+        }
+        return false;
+    }
+
+    /**
+     * 上传永久图文素材(认证后的订阅号可用)
+     * 新增的永久素材也可以在公众平台官网素材管理模块中看到
+     * @param array $data 消息结构{"articles":[{...}]}
+     * @return boolean|array
+     */
+    public function uploadForeverArticles($data){
+        if (!$this->access_token && !$this->checkAuth()) return false;
+        $result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_FOREVER_NEWS_UPLOAD_URL.'access_token='.$this->access_token,self::json_encode($data));
+        if ($result)
+        {
+            $json = json_decode($result,true);
+            if (!$json || !empty($json['errcode'])) {
+                $this->errCode = $json['errcode'];
+                $this->errMsg = $json['errmsg'];
+                return false;
+            }
+            return $json;
+        }
+        return false;
+    }
+
+    /**
+     * 修改永久图文素材(认证后的订阅号可用)
+     * 永久素材也可以在公众平台官网素材管理模块中看到
+     * @param string $media_id 图文素材id
+     * @param array $data 消息结构{"articles":[{...}]}
+     * @param int $index 更新的文章在图文素材的位置,第一篇为0,仅多图文使用
+     * @return boolean|array
+     */
+    public function updateForeverArticles($media_id,$data,$index=0){
+        if (!$this->access_token && !$this->checkAuth()) return false;
+        if (!isset($data['media_id'])) $data['media_id'] = $media_id;
+        if (!isset($data['index'])) $data['index'] = $index;
+        $result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_FOREVER_NEWS_UPDATE_URL.'access_token='.$this->access_token,self::json_encode($data));
+        if ($result)
+        {
+            $json = json_decode($result,true);
+            if (!$json || !empty($json['errcode'])) {
+                $this->errCode = $json['errcode'];
+                $this->errMsg = $json['errmsg'];
+                return false;
+            }
+            return $json;
+        }
+        return false;
+    }
+
+    /**
+     * 获取永久素材(认证后的订阅号可用)
+     * 返回图文消息数组或二进制数据,失败返回false
+     * @param string $media_id 媒体文件id
+     * @param boolean $is_video 是否为视频文件,默认为否
+     * @return boolean|array|raw data
+     */
+    public function getForeverMedia($media_id,$is_video=false){
+        if (!$this->access_token && !$this->checkAuth()) return false;
+        $data = array('media_id' => $media_id);
+        //#TODO 暂不确定此接口是否需要让视频文件走http协议
+        //如果要获取的素材是视频文件时,不能使用https协议,必须更换成http协议
+        //$url_prefix = $is_video?str_replace('https','http',self::API_URL_PREFIX):self::API_URL_PREFIX;
+        $result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_FOREVER_GET_URL.'access_token='.$this->access_token,self::json_encode($data));
+        if ($result)
+        {
+            if (is_string($result)) {
+                $json = json_decode($result,true);
+                if (isset($json['errcode'])) {
+                    $this->errCode = $json['errcode'];
+                    $this->errMsg = $json['errmsg'];
+                    return false;
+                }
+                return $json;
+            }
+            return $result;
+        }
+        return false;
+    }
+
+    /**
+     * 删除永久素材(认证后的订阅号可用)
+     * @param string $media_id 媒体文件id
+     * @return boolean
+     */
+    public function delForeverMedia($media_id){
+        if (!$this->access_token && !$this->checkAuth()) return false;
+        $data = array('media_id' => $media_id);
+        $result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_FOREVER_DEL_URL.'access_token='.$this->access_token,self::json_encode($data));
+        if ($result)
+        {
+            $json = json_decode($result,true);
+            if (!$json || !empty($json['errcode'])) {
+                $this->errCode = $json['errcode'];
+                $this->errMsg = $json['errmsg'];
+                return false;
+            }
+            return true;
+        }
+        return false;
+    }
+
+    /**
+     * 获取永久素材列表(认证后的订阅号可用)
+     * @param string $type 素材的类型,图片(image)、视频(video)、语音 (voice)、图文(news)
+     * @param int $offset 全部素材的偏移位置,0表示从第一个素材
+     * @param int $count 返回素材的数量,取值在1到20之间
+     * @return boolean|array
+     * 返回数组格式:
+     * array(
+     *  'total_count'=>0, //该类型的素材的总数
+     *  'item_count'=>0,  //本次调用获取的素材的数量
+     *  'item'=>array()   //素材列表数组,内容定义请参考官方文档
+     * )
+     */
+    public function getForeverList($type,$offset,$count){
+        if (!$this->access_token && !$this->checkAuth()) return false;
+        $data = array(
+            'type' => $type,
+            'offset' => $offset,
+            'count' => $count,
+        );
+        $result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_FOREVER_BATCHGET_URL.'access_token='.$this->access_token,self::json_encode($data));
+        if ($result)
+        {
+            $json = json_decode($result,true);
+            if (isset($json['errcode'])) {
+                $this->errCode = $json['errcode'];
+                $this->errMsg = $json['errmsg'];
+                return false;
+            }
+            return $json;
+        }
+        return false;
+    }
+
+    /**
+     * 获取永久素材总数(认证后的订阅号可用)
+     * @return boolean|array
+     * 返回数组格式:
+     * array(
+     *  'voice_count'=>0, //语音总数量
+     *  'video_count'=>0, //视频总数量
+     *  'image_count'=>0, //图片总数量
+     *  'news_count'=>0   //图文总数量
+     * )
+     */
+    public function getForeverCount(){
+        if (!$this->access_token && !$this->checkAuth()) return false;
+        $result = $this->http_get(self::API_URL_PREFIX.self::MEDIA_FOREVER_COUNT_URL.'access_token='.$this->access_token);
+        if ($result)
+        {
+            $json = json_decode($result,true);
+            if (isset($json['errcode'])) {
+                $this->errCode = $json['errcode'];
+                $this->errMsg = $json['errmsg'];
+                return false;
+            }
+            return $json;
+        }
+        return false;
+    }
+
 	/**
-	 * 上传图文消息素材(认证后的订阅号可用)
+	 * 上传图文消息素材,用于群发(认证后的订阅号可用)
 	 * @param array $data 消息结构{"articles":[{...}]}
 	 * @return boolean|array
 	 */

+ 9 - 2
wiki/官方API类库.md

@@ -130,8 +130,15 @@ const EVENT_CARD_USER_DEL = 'user_del_card';        //卡券 - 用户删除卡
  *  getServerIp() 获取微信服务器IP地址列表 返回数组array('127.0.0.1','127.0.0.1')
  *  getMenu() 获取菜单 
  *  deleteMenu() 删除菜单 
- *  uploadMedia($data, $type) 上传多媒体文件(注意上传大文件时可能需要先调用 set_time_limit(0) 避免超时)
- *  getMedia() 获取接收到的音频、视频媒体文件 
+ *  uploadMedia($data, $type) 上传临时素材,有效期为3天(注意上传大文件时可能需要先调用 set_time_limit(0) 避免超时)
+ *  getMedia($media_id,$is_video=false) 获取临时素材(含接收到的音频、视频媒体文件)
+ *  uploadForeverMedia($data, $type,$is_video=false,$video_info=array()) 上传永久素材,可以在公众平台官网素材管理模块中看到
+ *  uploadForeverArticles($data) 上传永久图文素材
+ *  updateForeverArticles($media_id,$data,$index=0) 修改永久图文素材(认证后的订阅号可用)
+ *  getForeverMedia($media_id,$is_video=false) 获取永久素材
+ *  delForeverMedia($media_id) 删除永久素材
+ *  getForeverList($type,$offset,$count) 获取永久素材列表(认证后的订阅号可用)
+ *  getForeverCount() 获取永久素材总数
  *  uploadMpVideo($data) 上传视频素材,当需要群发视频时,必须使用此方法得到的MediaID,否则无法显示
  *  uploadArticles($data) 上传图文消息素材
  *  sendMassMessage($data) 高级群发消息