微信小程序推送模版消息发送代码

11 篇文章 0 订阅

写的是自己看得懂的记录。未经过整理

//频道动态详情
		$info = db('dynamic')->field('id,user_id,title,point,form_id,use_form_id')->where('id',$dynamic_id)->find();

		//推送
		if(!empty($info['form_id']) && $info['use_form_id'] != 1){
			$user_data = db('user')->field('xcx_openid,user_realname')->where(['id'=>$info['user_id']])->find();
			$toUser = $user_data['xcx_openid'];
			$user_realname = $user_data['user_realname'];//点赞人
			$template_id = 'N4zvdShk*************************Ka_Q_zaj7owqPLI';
			$json = '{
	            "keyword1": {
	              "value": "'.$info['title'].'", 
	              "color": "#173177",
	              "size": "10"
	            }, 
	            "keyword2": {
	              "value": "'.$user_realname.'", 
	              "color": "#173177"
	            }, 
	            "keyword3": {
	              "value": "'.date('Y-m-d H:i:s',time()).'", 
	              "color": "#173177"
	            }
	        }';
			$this->tuisong($toUser,$info['form_id'],$json,$template_id,$dynamic_id);
		}
public function tuisong($toUser,$form_id,$json,$template_id,$dynamic_id){
    	// 微信设置
        $weixinInfos = cmf_get_weixin_infos();
        $appId =isset($weixinInfos['xcx_appid'])?$weixinInfos['xcx_appid']:'';
        $appsecret = isset($weixinInfos['xcx_appsecret'])?$weixinInfos['xcx_appsecret']:'';

		$access_token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appId&secret=$appsecret";
		$result = $this->_get($access_token_url);
		$token_info = json_decode($result, true);
		$accesstoken = $token_info['access_token'];

		$url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=".$accesstoken;

		if(!empty($toUser) || !empty($form_id)){ 
			$j= '{
					  "touser": "'.$toUser.'",  
					  "template_id": "'.$template_id.'",  
					  "page": "",          
					  "form_id": "'.$form_id.'",         
					  "data": '.$json.'
					}';
			$curl = curl_init();

			curl_setopt_array($curl, array(
				CURLOPT_URL => $url,
				CURLOPT_SSL_VERIFYPEER => FALSE,
				CURLOPT_SSL_VERIFYHOST => FALSE,
				CURLOPT_RETURNTRANSFER => true,
				CURLOPT_ENCODING => "",
				CURLOPT_MAXREDIRS => 10,
				CURLOPT_TIMEOUT => 30,
				CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
				CURLOPT_CUSTOMREQUEST => "POST",
				CURLOPT_POSTFIELDS => $j,
				CURLOPT_HTTPHEADER => array(
					"Cache-Control: no-cache",
					"Content-Type: application/json",
				),
			));

			$response = curl_exec($curl);
			$err = curl_error($curl);

			curl_close($curl);
			
			db('dynamic')->where(['id'=>$dynamic_id])->update(['use_form_id' => 1]);//更新推送id为已使用

			if ($err) {
			  	error_log('点赞失败:'.print_r($err,1)."\r\n",3,'dynamic_like.log');
			} else {
			 	error_log('点赞成功:'.print_r($response,1)."\r\n",3,'dynamic_like.log');
			}

		}
		
	}
/**
	 * 模拟GET请求
	 */
	public function _get($url){
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);         //请求URL
		curl_setopt($ch, CURLOPT_HEADER, 0);         //过滤HTTP头
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//SSL证书认证
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
		curl_setopt($ch, CURLOPT_TIMEOUT, 5);
		$rs = curl_exec($ch);
		if (curl_errno($ch)) {
			curl_close($ch);
			return false;
		} else {
			curl_close($ch);
			return $rs;
		}
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

每天都进步一点点

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值