顶部导航开始 -->

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

//获取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;
}


自学php博客
请先登陆后发表评论
  • 最新评论
  • 总共0条评论
  • © 2014-2016 zixuephp.cn 版权所有
  • ICP证:皖ICP备16012107号
  • 联系邮箱:510082587@qq.com
  • 交流请加微信:weibobf2899 或扫下方二维码