微信模板推送

微信模板推送其实是相对简单的。微信的相关的模板的建立在这里我就不说了,直接去查看文档就好了。模版消息只对认证的服务号开放。

后台数据的处理

模版消息调用时主要需要模版ID和模版中各参数的赋值内容。请注意: 1.模版中参数内容必须以".DATA"结尾,否则视为保留字; 2.模版保留符号"{{ }}"

<?php
class index{
	
	var $appid="wx1XXXXXXXXXXX1bc";
	var $sercet_app="03XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX7bb";
	var $access_token;
	function __construct()
	{
			$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->sercet_app;
			//get获取access_token的值
			$data=$this->curl_get($url);
			$res=json_decode($data);
			$this->access_token=$res->access_token;
	}
		
	function root()
	{
			//模板消息
			$json_template = $this->json_tempalte();
			$url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$this->access_token;
			$res=$this->postJson($url,urldecode($json_template));
	}
		
	function json_tempalte()
	{
			//模板消息
			$template=array(
					'touser'=>'o3XXXXXXXXXXXXXXXXXXXglQ',  //用户openid
					'template_id'=>"N0ur9yXXXXXXXXXXXXXXXXXXXXXY3ZrFv8", //在公众号下配置的模板id
					'url'=>"www.XXXXXXXXXXX", //点击模板消息会跳转的链接
					'topcolor'=>"#7B68EE",
					'data'=>array(
							'dept'=>array('value'=>urlencode("节操充值中心"),'color'=>"#FF0000"),
							'event'=>array('value'=>urlencode('您的节操已透支'),'color'=>'#FF0000'),  //keyword需要与配置的模板消息对应
							'no'=>array('value'=>urlencode('请及时充值'),'color'=>'#FF0000'),
							'status'=>array('value'=>urlencode('爱谁谁'),'color'=>'#FF0000'),
							'startdate'=>array('value'=>urlencode('2019-01-01'),'color'=>'#FF0000'),
							'enddate'=>array('value'=>urlencode('2019-02-28'),'color'=>'#FF0000'),
							'remark' =>array('value'=>urlencode('备注:爱充不充'),'color'=>'#FF0000'), )
			);
			$json_template=json_encode($template);
			return $json_template;
	}
	
	function curl_get($url)
	{
	    	$ch = curl_init();
	    	// 必须的四个配置
	    	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	    	curl_setopt($ch, CURLOPT_HTTPHEADER, array(
	    			'Content-Type: application/json;charset=utf-8;'
	    	));
	    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	    	curl_setopt($ch, CURLOPT_URL, $url);
	    	// curl_setopt($ch, CURLOPT_POST, 1);
	    	// curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
	    	$content = curl_exec($ch);
	    	return $content;
	 }
	 
	function postJson($url, $json, $type = "array")
	 {
	    	try {
	    		$ch = curl_init();
	    		// 必须的四个配置
	    		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	    		curl_setopt($ch, CURLOPT_HTTPHEADER, array(
	    				'Content-Type: application/json;charset=utf-8;'
	    		));
	    		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	    		curl_setopt($ch, CURLOPT_URL, $url);
	    		curl_setopt($ch, CURLOPT_POST, 1);
	    		curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
	    		$content = curl_exec($ch);
	    	} catch (HttpException $ex) {
	    		return $ex . "<-Exception";
	    	}
	    	if ($type == 'array') {
	    		return json_decode($content, true);
	    	} else {
	    		return $content;
	    	}
	    }	
	}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值