素材管理(添加图片和图文)

添加图片:(创建一个image_submit方法)

首先要获取图片的路径,添加图片有2种(临时、永久),他们的接口不同,所以下一步要判断添加的图片是永久还是临时,然后在获取access_token.

//图片在服务器上的路径
		$url = I('post.url');
		//将相对路径转化为绝对路径
		$file = realpath('.' . $url);
		// echo $file;
		// exit;
		//得到上传类型临时还是永久
		$staus_type = I('post.staus_type');

		//获取access_tkoen
		$access_Token = getAccess_token();

		include_once APP_PATH . 'LaneWeChat/lanewechat.php';
		//判断是临时还是永久
		if($staus_type ==0){
			$url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=$access_Token&type=image";
		}else{
			$url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=$access_Token&type=image";
		}

		$data['media']= Curl::addFile($file);
		$ret = Curl::callWebServer($url,$data,'post',true,false);

上传成功后会返回media_id和url(公众号经常有需要用到一些临时性的多媒体素材的场景,是通过media_id来进行的。),所以要判断是否存在media_id,存在就往数据库里添加数据

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();

			$model = M('material')->add($data);
			$this->ajaxReturn(array('msg'=>'上传成功'));
		}else{
			$this->ajaxReturn(array('msg'=>$ret));
		}
	}

添加图文:

上传图片的部分相同,(添加的图片是永久的)

$url = I('post.url');

		//将相对路径改为绝对路径
		$file = realpath('.' . $url);
		// echo $file;
		// exit;

		$title = I('post.title');
		$content = I('post.content');
		$link = I('post.link');

		//获取access_tkoen
		$access_Token = getAccess_token();

		include_once APP_PATH . 'LaneWeChat/lanewechat.php';
		//传一个永久的图片
		$url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=$access_Token&type=image";

		$data['media']= Curl::addFile($file);
		$ret = Curl::callWebServer($url,$data,'post',true,false);
		// $this->ajaxReturn($ret);
		// exit;

上传文字要根据一定的格式(调用示例

 

调用示例

{
"articles": [{
"title": TITLE,
"thumb_media_id": THUMB_MEDIA_ID,
"author": AUTHOR,
"digest": DIGEST,
"show_cover_pic": SHOW_COVER_PIC(0 / 1),
"content": CONTENT,
"content_source_url": CONTENT_SOURCE_URL
},
//若新增的是多图文素材,则此处应还有几段articles结构
]
}

所以先创建一个数组,将数据存入数组中,在转化为json格式。

$arr = array();
		if(isset($ret['media_id'])){
			$arr["title"] = $title;
			$arr["thumb_media_id"] = $ret['media_id'];
			$arr["author"] = 'jh';
			$arr["digest"] =  'bbb';
			$arr["show_cover_pic"] = 1;
			$arr["content"] = $content;
			$arr["content_source_url"] = $link;
		}
		// print_r($arr);
		// exit;
		$data["articles"][] = $arr;
		// print_r($data);
		// exit; 
		$data = json_encode($data,JSON_UNESCAPED_UNICODE);
		// dump($data);
		// exit;

对比转化后的是否和上边格式相同,相同后调用添加图文素材接口(成功返回的即为新增的图文消息素材的media_id。)

判断是否存在media_id,如果存在将上边的数组添加到数据库

		$url = "https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=$access_Token";
		$ret = Curl::callWebServer($url,$data,'post',true,false);

		if(isset($ret['media_id'])){
			$mp = $this->mp;
			$arr['mp_id'] = $mp['id'];
			$arr['url'] = $ret['url'];
			// print_r($arr);
			// exit;
			$model = M('material')->add($arr);
			$this->ajaxReturn(array('msg'=>'上传成功'));
		}else{
			$this->ajaxReturn($ret);
		}
		

部分摘自微信开发者文档......

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值