1.配置网页授权的回调域名
2.基本授权(snsapi_base) 创建weiixn/index.php
<?php
if(isset($_GET['state'])){
getUserOpenid();
}else{
getBaseInfo();
}
//1.获取到code
function getBaseInfo(){
$APPID='wx5ca785d746a72d7a';
//授权后重定向的回调链接地址, 请使用 urlEncode 对链接进行处理
$redirect_uri=urlencode('http://192.168.1.119/weixin/index.php');
$url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$APPID."&
redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_base&state=123#wechat_redirect";
header('location:'.$url);
}
//2.获取到网页授权的access_token(获取用户详细信息时需要),openid,实现基本授权
function getUserOpenid(){
$APPID='wx5ca785d746a72d7a';
$APPSECRET='a28da942950c5bf8036bba2fa029cc15';
$CODE=$_GET['code'];
var_dump($CODE);
$url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$APPID."&
secret=".$APPSECRET."&code=".$CODE."&grant_type=authorization_code";
$res=httpsGet( $url);
$arr=json_decode($res,true);
var_dump($arr['openid']);
//到这里就已经完成了基本授权,可以开始业务处理
}
function httpsGet($url){
$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); //
$json = curl_exec($ch); //获取
curl_close($ch);
return $json;
}
微信中访问:http://192.168.1.119/weixin/index.php
3.详细授权 (snsapi_userinfo) 创建weiixn/detail.php
<?php
if(isset($_GET['state'])){
getUserOpenid();
}else{
getBaseInfo();
}
//1.获取到code
function getBaseInfo(){
$APPID='wx5ca785d746a72d7a';
//授权后重定向的回调链接地址, 请使用 urlEncode 对链接进行处理
$redirect_uri=urlencode('http://192.168.1.119/weixin/detail.php');
$url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$APPID."&
redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect";
header('location:'.$url);
}
//2.获取到网页授权的access_token(获取用户详细信息时需要),openid,实现基本授权
function getUserOpenid(){
$APPID='wx5ca785d746a72d7a';
$APPSECRET='a28da942950c5bf8036bba2fa029cc15';
$CODE=$_GET['code'];
var_dump($CODE);
$url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$APPID."&
secret=".$APPSECRET."&code=".$CODE."&grant_type=authorization_code";
$res=httpsGet( $url);
$arr=json_decode($res,true);
$access_token=$arr['access_token'];
$openid=$arr['openid'];
//到这里就已经完成了授权,进一步拉取用户信息
$url='https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&
openid='.$openid.'&lang=zh_CN';
$res=httpsGet( $url);
$arr=json_decode($res,true);
var_dump($arr);
}
function httpsGet($url){
$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); //
$json = curl_exec($ch); //获取
curl_close($ch);
return $json;
}
微信中访问:http://192.168.1.119/weixin/detail.php