微信第三方素材管理

微信公众号经常有需要用到一些临时性的多媒体素材的场景,例如在使用接口特别是发送消息时,对多媒体文件、多媒体消息的获取和调用等操作,这些是通过media_id来进行的。素材管理接口对所有认证的订阅号和服务号开放。通过本接口,公众号可以新增临时素材(即上传临时多媒体文件)。媒体文件在微信后台保存时间为三天,即在三天后media_id就会失效。

素材的格式分为:图片(支持bmp/png/jpeg/jpg/gif格式、语音(播放长度不超过60s,mp3/wma/wav/amr格式)、视频(支持MP4格式)、缩略图(64KB,支持JPG格式)。下面是新增临时素材和永久素材的两个方法:

1.images_submit()方法是新增图片素材

	public function image_submit(){
		$url=I('post.url');//图片在本地服务器上的路径		
		$file=realpath('.' .$url);// 相对路径换位结对路径
		$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']='@' .$file;
        // $data['media']=Curl::addFile($file);
        $ret=Curl::callWebServer($url,$data,'post',true,false);
       
        if(isset($ret['media_id'])){
           $mp=$this->$mp;
           $mp=getCurrentMp();
           $mp_id=$mp['id'];
           $data['mp_id']=$mp['id'];
           $data['type']='image';
           $data['url']=$url;
           $data['media_id']=$ret['media_id'];
           M('material')->add($data);
           $this->ajaxReturn(array('msg'=>'上传成功'));
        }else{
        $this->ajaxReturn(array('msg'=>$ret));
    }

}

2.news_submit()方法是新增图文素材

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']='@'.$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'=>'awm',
                   'digest'=>'aaa',
                   'show_cover_pic'=>1,
                   'content'=>$content,
                   'content_source_url'=>$link,
               	);

               $data['articles'][]=$arr;
               $data=json_encode($data,JSON_UNESCAPED_UNICODE);             
             
               $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=getCurrentMp();
	           $mp_id=$mp['id'];
	           $data['mp_id']=$mp['id'];
	           $data['type']='news';
	           $data['url']=$api;
	           $data['content']=$content;
	           $data['description']=$description;
	           $data['media_id']=$ret['media_id'];
	           $data['author']=$author;
	           M('material')->add($data);           
	        }
               $this->ajaxReturn($ret);
		}else{
			$this->ajaxReturn($ret);
		}			

	}

上面两个方法是新增素材的方法,还有一个upload方法是通过前台页面来接收上传文件的方法:

3.接收上传文件upload方法

      //接收文件上传
	public function upload(){
		$upload=new \Think\Upload();//实例化上传类
	    $upload->maxSize =3145728;//设置上传附件大小
	    $upload->exts=array('jpg','gif','png','jpeg');//设置附件上传类型
	    $upload->rootPath='./Upload/';//设置附件上传跟目录
	    $upload->savePath='';//设置附件上传(子)目录
	    $info=$upload->uploadOne($_FILES['file']);
	    if(!$info){
	    	//上传错误提示错误信息
	    	$this->ajaxReturn(array('code'=>1,'msg'=>$upload->getError()));
	    }else{
	    	//上传成功
	    	$file='/Upload/'.$info['savepath'].$info['savename'];
	    	$this->ajaxReturn(array('code'=>0,'msg'=>'上传成功','url'=>$file));
	    }
	}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值