public function Wxchat(){
//获取到code
//公众号的appid
$appid = "微信公众号的appid";
//回调地址
$redirect_uri = urlencode("http://授权回调域名/index.php/Home/Index/getWxCode");
//拼接字符串,并赋值给 $url
$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";
//跳转到 url
header("location:".$url);
}
public function getWxCode(){
//获取网页授权的access_token
//公众号的appid
$appid = "微信公众号的appid";
//公众号的appsecret
$appsecret = "微信公众号的appsecret";
//传递过来的code
$code = $_GET["code"];
//拼接字符串,并赋值给 $url
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&
code=".$code."&grant_type=authorization_code";
//请求 $url并返回json数据
$res = file_get_contents($url);
dump($res);
}
public function getUserDetail(){
//用户同意授权,获取code
$appid = "微信公众号的appid";
//回调地址
$redirect_uri = urlencode("http://授权回调域名/index.php/Home/Index/getUserInfo");
//在确保微信公众账号拥有授权作用域(scope参数)的权限的前提下(服务号获得高级接口后,
默认拥有scope参数中的snsapi_base和snsapi_userinfo)
$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";
//跳转到 $url
header("location:".$url);
}
public function getUserInfo(){
$appid = "微信公众号的appid";
$appsecret = "微信公众号的appsecret";
$code = $_GET["code"];
//获取网页授权的access_token
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&
code=".$code."&grant_type=authorization_code";
//请求 $url 返回一个json json_decode不加 true 会将json转为对象,加true转为数组
$res = json_decode(file_get_contents($url),true);
//获取access_token并赋值给变量
$access_token = $res["access_token"];
//获取openid并赋值给变量
$openid = $res["openid"];
//拼接字符串并赋值给 $urls
$urls = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
//请求用户详细信息,并赋值给 $userinfo
$userinfo = file_get_contents($urls);
}