关于微信网页授权
时间:2023-05-23 12:33:02 | 来源:网站运营
时间:2023-05-23 12:33:02 来源:网站运营
关于微信网页授权:我自己亲测可行 下面贴代码,希望能给需要帮助的人以帮助
class WeixinController
extends Controller{
public $appId =
'wx...';
public $appSecret =
'f3....';
public function getaccesstoken()
{
$js_code = $_GET[
'code'];
if($js_code){
//dump($js_code);
$url =
"https://api.weixin.qq.com/sns/oauth2/access_token?appid=$this->appId&secret=$this->appSecret&code=$js_code
&grant_type=authorization_code";
//dump($url);
$res = WeixinController::
getJson($url);
$openid = $res[
'openid'];
$access_token = $res[
'access_token'];
$url1 =
"https://api.weixin.qq.com/sns/userinfo?access_token=$access_token
&openid=$openid
&lang=zh_CN ";
$date = WeixinController::
getJson($url1);
//dump($date);die;
$_SESSION[
'headImgUrl'] = $date[
'headimgurl'];
$_SESSION[
'nickName'] = $date[
'nickname'];
dump($_SESSION[
'headImgUrl']);
if(session(
'headImgUrl')){
$this->redirect(
'Login/login');
}
}
else{
echo 'code没有相应';
}
}
public static 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);
}
}