微信中的素材管理

素材管理中新增素材包括永久素材和临时素材。临时素材media_id是可复用的,可新增图片,音频,缩略图,语音,需要用https调用接口,请求方式为post,其中图片可以是临时的也可以是永久的,在新增时首先注意要获取当前正在使用的公众号以及access_token。

新增图片以及图文:例:在MaterialController.class.php中,1.先写一个方法获取当前使用的公众号:

<?php
namespace Home\Controller;
use Think\Controller;
use LaneWeChat\Core\Curl;
class MaterialController extends Controller{

	private $mp;
	public function _initialize(){
        $mp = getCurrentMp();
		if(empty($mp)){
			$this->error('当前无使用公众号',U('mp/index'));
			exit;
		}else{
			$this->mp = $mp;
		}
	}

2.其次,写一个素材管理页面和新增素材页面的方法:

// 显示素材管理页面
	public function index($type=''){
		if(empty($type)){
			$type='image';
		}
        $this->assign('type',$type);
		$this->display();
	}
//显示新增素材页面
	public function addmeterial($type=''){
		if(empty($type)){
			$type='image';
		}
		$this->assign('type',$type);
		$this->assign('action',$type.'_submit');
		$this->display();
	}

3.新增素材即上传文件或图片,所以还要写一个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->error($upload->getError());
			$this->ajaxReturn(array('code'=>1,'msg'=>$upload->getError()));
		}else{// 上传成功
			// $this->success('上传成功!');
			$file = '/Uploads/'.$info['savepath'].$info['savename'];
			$this->ajaxReturn(array('code'=>0,'msg'=>'上传成功','url'=>$file));
		}
	}

4.上传图片的方法,因为上传图片可以是临时的也可以是永久的所以就用到了两个接口,根据选择的类型判断调用那个接口:

//图片上传公众号服务器
	public function image_submit(){
        $url = I('post.url');//获取图片在本服务器上的地址
        //将相对路径改为绝对路径
        $file = realpath('.' . $url);
        // echo $file;
        // exit;
        $staus_type = I('post.staus_type');

        //获取access_token
        $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['mpid'] = $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('图片上传成功'));
        }

        $this->ajaxReturn($ret);
	}

5.上传图文的方法:图文中的图片为永久素材,所以图片的接口要调用永久素材的接口,其次再调用图文的接口:

//图文上传公众号服务器
	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();//获取当前公众号的access_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);
        // $this->ajaxReturn($ret);
        // exit;
        if(isset($ret['media_id'])){
        	$arr = array(
        	'title'=>$title,
        	'thumb_media_id'=>$ret['media_id'],
        	'author'=>'yyy',
        	'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['mpid'] = $mp['id'];
        			$arr['title'] = $title;
        			$arr['type'] = 'news';
        			$arr['content'] = $content;
                    $arr['url'] = $url;
                    $arr['create_time'] = time();
                    $arr['link'] = $link;
                    $arr['media_id'] = $ret['media_id'];
        		}
        		M('material')->add($arr);
        	    $this->ajaxReturn(array('图文上传成功'));
        }else{
        	$this->ajaxReturn($ret);
        }
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值