tp5 对接公众号推送模板消息

首先第一步是要配置好APPID和Secret

在config.php里面进行配置,然后在控制器里面通过 
	$this->wx_config = Config::get('weChat');
   	 $this->appid = $this->wx_config['AppId'];
    $this->secret = $this->wx_config['AppSecret'];
    来获取,记得use \think\Config;	

第二步通过code获取到openID

//获取code
public function index()
{
    //1.获取到code=================================
    $redirect_uri = urlencode("http://" . $_SERVER['HTTP_HOST'] . "/index/Api/saveOpenid");//这里的地址需要http://
    $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $this->appid . "&redirect_uri=" . $redirect_uri . "&response_type=code&scope=snsapi_base&state=123#wechat_redirect";
    $this->redirect($url);
    exit();
}
//存储

public function saveOpenid()
{
    $data = request()->get();
    $code = $data['code'];
    $token = getAccess_token($this->appid,$this->secret);//这里是获取access_oken
    //第二步:取得openid
    $oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$this->appid&secret=$this->secret&code=$code&grant_type=authorization_code";
    $oauth2 = getJson($oauth2Url);
    //第三步:根据全局ac
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值