素材管理,主要围绕新增临时素材和永久素材展开:
(1)、新增临时素材文件(订阅号与服务号认证后均可用)
a.需要注意的问题:
1、临时素材media_id是可复用的。
2、媒体文件在微信后台保存时间为3天,即3天后media_id失效。
3、需使用https调用本接口。
http请求方式:POST/FORM,使用https
https://api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE
type - 媒体文件类型,分别有图片(image)、语音(voice)、视频(video)和缩略图(thumb)
返回说明
正确情况下的返回JSON数据包结果如下:
{"type":"TYPE","media_id":"MEDIA_ID","created_at":123456789}
错误情况下的返回JSON数据包示例如下(示例为无效媒体类型错误):
{"errcode":40004,"errmsg":"invalid media type"}
返回值:
{"type":"TYPE","media_id":"MEDIA_ID","created_at":123456789}
// 区别于 "永久素材",返回了 "创建时间",我们可以定位素材的过期时间还剩多久。
(2)、新增永久素材
a:需要注意的问题:
1.最近更新:永久图片素材新增后,将带有URL返回给开发者,开发者可以在腾讯系域名内使用(腾讯系域名外使用,图片将被屏蔽)
2.当上传永久素材时返回的url,在地址栏中进行访问时,可以访问到你上传的图片
接口调用请求说明
http请求方式: POST,https协议
https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=ACCESS_TOKEN
返回说明
{
"media_id":MEDIA_ID
}
示例代码:
public function image_submit(){
$url=I('post.url');//图片在本地服务器上的路径
// dump($url);
// exit;
$file=realpath('.'. $url);//相对路径-》绝对路径
// dump($file);
// exit;
$staus_type=I('post.staus_type');//临时,永久
$accessToken=getAccess_token();
include APP_PATH . 'LaneWeChat/lanewechat.php';
if($staus_type==0){
$url="https://api.weixin.qq.com/cgi-bin/media/upload?access_token=$accessToken&type=image";
}else{
$url="https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=$accessToken&type=image";
}
$data['media']=Curl::addFile($file);
$ret=Curl::callWebServer($url,$data,'post',1,0);
// $this->ajaxReturn($ret);
if(isset($ret['media_id'])){
$model=M('material');
$mp=getCurrentMp();
$arr=array(
'mpid'=>$mp['id'],
'media_id'=>$ret['media_id'],
'create_time'=>time(),
'url'=>$file,
'type'=>'image'
);
$ret=$model->add($arr);
if($ret){
$this->ajaxReturn(array('ret'=>true,'msg'=>'上传成功'));
}else{
$this->ajaxReturn(array('ret'=>false,'msg'=>'上传失败'));
}
}
}