微信中新增素材管理分为临时素材和永久素材。在新增素材时,会有新增图片和新增图文。
临时素材的api接口:
$api=https://api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE;
永久素材的api接口:
$api=https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=ACCESS_TOKEN;
在新增图片和图文上传公众号服务时,首先需获取当前使用的公众号,代码如下:
private $mp; public function _initialize(){ $mp=getCurrentMp(); if(empty($mp)){ $this->error('无使用的公众号',U('mp/index')); exit; }else{ $this->mp=$mp; } }新增图片上传公众号服务(图片上传有临时素材和永久素材)代码如下:
public function image_submit(){ $url=I('post.url');//图片在本地服务器上的路径 // 相对路径->绝对路径 $file=realpath('.'.$url); // echo $file; // exit; $staus_type=I('post.staus_type'); $accessToken=getAccess_token(); include APP_PATH.'LaneWeChat/lanewechat.php'; if($staus_type==0){ // 临时素材 $api="https://api.weixin.qq.com/cgi-bin/media/upload?access_token=$accessToken&type=image"; }else{ // 永久素材 $api="https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=$accessToken&type=image"; } $data['media']=Curl::addFile($file); $ret=Curl::callWebServer($api,$data,'post',true,false); //存入数据库 if(isset($ret['media_id'])){ $mp=$this->mp; $data['mp_id']=$mp['id']; $data['type']='image'; $data['url']=$url; $data['media_id']=$ret['media_id']; $data['create_time']=time(); M('material')->add($data); $this->ajaxReturn(array('msg'=>'上传成功')); }else{ $this->ajaxReturn(array('msg'=>$ret)); } }新增图文上传公众号服务代码如下:
public function news_submit(){ $url=I('post.url'); $file=realpath('.'.$url); $title=I('post.title');//标题 $content=I('post.content');//内容 $link=I('post.link');//链接 $accessToken=getAccess_token(); include APP_PATH.'LaneWeChat/lanewechat.php'; //上传永久图片api $api="https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=$accessToken&type=image"; $data['media']=Curl::addFile($file); $ret=Curl::callWebServer($api,$data,'post',true,false); if(isset($ret['media_id'])){ $arr=array( 'title'=>$title, 'thumb_media_id'=>$ret['media_id'], 'author'=>'ff', 'digest'=>'aaa', 'show_cover_pic'=>1, 'content'=>$content, 'content_source_url'=>$link ); $data['articles'][]=$arr; $data=json_encode($data,JSON_UNESCAPED_UNICODE); // echo $data; // exit; $api="https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=$accessToken"; $ret=Curl::callWebServer($api,$data,'post',true,false); if(isset($ret['media_id'])){ $mp=$this->mp; $arr['mp_id']=$mp['id']; $arr['title']=$title; $arr['url']=$url; $arr['link']=$link; $arr['content']=$content; $arr['create_time']=time(); M('material')->add($arr); $this->ajaxReturn(array('msg'=>'上传成功')); }else{ $this->ajaxReturn(array('msg'=>$ret)); } } }上传文件时,需有upload方法,用来上传文件,代码如下:
public function upload(){ $upload = new \Think\Upload();// 实例化上传类 $upload->maxSize = 3145728 ;// 设置附件上传大小 $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型 $upload->rootPath = './Uploads/'; // 设置附件上传根目录 $upload->savePath = ''; // 设置附件上传(子)目录 // 上传文件 $info = $upload->uploadOne($_FILES['file']); if(!$info) {// 上传错误提示错误信息 $this->ajaxReturn(array('code'=>1,'msg'=>$upload->getError())); }else{// 上传成功 $file='/Uploads/'.$info['savepath'].$info['savename']; $this->ajaxReturn(array('code'=>0,'msg'=>"上传成功",'url'=>$file)); } }