• 河南网站建设-郑州网站设计-郑州网站建设-手机建站-郑州建站-上海建站

  • 专注网站建设 服务热线: 13061801310

当前位置:东东网 > 近期动态 > 建站常见问题 > 正文

微信公众号接入一次性订阅消息

发布时间:2020-04-26 | 发布者: 东东工作室 | 浏览次数:
//获取access_token
$appid=$res['appid'];
$appsecret=$res['appsecret'];	
$access_token=$this->get_token_name($appid,$appsecret);

//发送模板消息 $url 接口调用请求
$url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
$this->send_messages($teamid,$uid,$openid,$head_name,$shop_name,$goods_name,$times,$foot_name,$url);


/*获取access_token*/
public function get_token_name($appid,$appsecret){
	$access_token_url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
	// 初始化curl
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $access_token_url);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
      // 设置header
      curl_setopt($ch, CURLOPT_HEADER, FALSE);
      // 要求结果为字符串且输出到屏幕上
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
      // post提交方式  $post参数不填则为get
      curl_setopt($ch, CURLOPT_POST, TRUE);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
      // 运行curl
      $data = curl_exec($ch);
	$arr = json_decode($data, true); //将结果转为数组
	$_SESSION['access_token']=$arr['access_token'];
	$access_token=$arr['access_token'];
	return $access_token;
}


//发送模板消息
public function send_messages($teamid,$uid,$openid,$head_name,$shop_name,$goods_name,$times,$foot_name,$url){
	$post=array(
		'touser'=>$openid,
		'template_id'=>"cf6Qi-XgQeItMkPYqBzz8ctKCI9bcznjingLl8J0kSU",    //模板的id
		'url'=>"http://xxx.com/index.php?m=team&c=goods&a=panicwait&team_id=".$teamid."&user_id=".$uid,	
		'data'=>array(
			'first'=>array('value'=>urlencode($head_name),'color'=>"#000"),        
			'keyword1'=>array('value'=>urlencode($shop_name),'color'=>'#000'),        
			'keyword2'=>array('value'=>urlencode($goods_name),'color'=>'#000'),   
			'keyword3'=>array('value'=>urlencode($times),'color'=>'#000'),   
			'remark'=>array('value'=>urlencode($foot_name),'color'=>'#000'),
		)
	);
	$json_template=json_encode($post);

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, urldecode($json_template));
	$output = curl_exec($ch);
	curl_close($ch);
	return $output;
}

转载请标注:东东工作室——微信公众号接入一次性订阅消息