微信网页授权获取用户信息

微信网页授权

 $code=$_GET['code'];
if (empty($code)) {          header("location:https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . APPID . "&redirect_uri=" . REDIRECT_URI . "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect");
            exit();
        }  

//获取用户信息
    function getinfo($code) {
        //第一步:取得openid
        $oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . APPID . "&secret=" . APPSECRET . "&code=" . $code . "&grant_type=authorization_code";
        $oauth2 = getJson($oauth2Url);

        //第二步:根据全局access_token和openid查询用户信息  
        $access_token = $oauth2["access_token"];
        $openid = $oauth2['openid'];
        $get_user_info_url = "https://api.weixin.qq.com/sns/userinfo?access_token=" . $access_token . "&openid=" . $openid . "&lang=zh_CN";
        $userinfo = getJson($get_user_info_url);
}
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);
    }

$data=getinfo($code);
var_dump($data);

如果只要获取openid,可更改跳转链接。

微信网页授权获取用户信息:等您坐沙发呢!

发表评论

表情
还能输入210个字