php微信号授权登录失败,php微信授权登录

该代码实现了一个基于微信OAuth2.0的授权流程,用于获取用户的appid、appsecret、code,并进一步获取用户的openid和unionid。在用户授权后,将信息存储到数据库并重定向至指定URL。
摘要由CSDN通过智能技术生成

新建文件 userinfo.php

private $appId;

private $appSecret;

public function __construct(){

$this->appId = '';

$this->appSecret= '';

}

//获取code

public function getCode(){

//scope=snsapi_base 实例

$unique_id = $_GET["unique_id"];

$appid = $this->appId;

$redirect_uri = urlencode (config('WEB_URL').'/home/userinfo/getUserInfo?unique_id='.$unique_id);

$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";

header("Location:".$url);

}

//获取用户信息

public function getUserInfo(){

$appid = $this->appId;

$secret = $this->appSecret;

$code = $_GET["code"];

//自己定义需要携带的参数unique_id

$unique_id = $_GET["unique_id"];

//第二步:取得openid

$oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";

$oauth2 = $this->getJson($oauth2Url);

$data = [

'unique_id' => $unique_id,

'openid' => $oauth2['openid'],

'unionid' => $oauth2['unionid'],

'create_time' => date("Y-m-d H:i:s")

];

Db::table('tc_share')->insert($data);

$redirect_url = 'https://a.app.qq.com/o/simple.jsp?pkgname=com.hdn.tici';

header("Location:".$redirect_url);

exit;

}

//请求

public function getJson($url){

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$output = curl_exec($ch);

curl_close($ch);

return json_decode($output, true);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值