发新话题
打印

微信第三方平台网页授权获取用户基本信息

微信第三方平台网页授权获取用户基本信息

第一步,要获取授权code
header('Location: https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxb0bbf1c15ff341e5&redirect_uri=http%3A%2F%2Fwxtest.52cp.com%2Fwx1119b702219f8e0e%2F&response_type=code&scope=snsapi_userinfo&state=nouser&component_appid=wxfe673b2e38dc525c#wechat_redirect');
component_appid:第三方平台生成的appid;
第二步,通过code换取access_token

            $code = $_GET['code'];
            $url = 'https://api.weixin.qq.com/sns/oauth2/component/access_token';
            $postData = array('appid'=>'wxb0bbf1c15ff341e5','code'=>$code,'grant_type'=>'authorization_code','component_appid'=>'wxfe673b2e38dc525c','component_access_token'=>'Yqb2kUkXN37CfIAFGAs-eeLaT2tlVGQNyrTFg8chYCx7arLZAifOV4w6kXb5hP81cXl4Wz16ElH8C6R2Vh38URietxjaSQ3UkVbwCFiVVd9IWrU5ATckyB1lc1NoKHq6CCVgABAUUN');
            $responseObj = json_decode(curlPost($url, $postData), true);
            if(isset($responseObj['openid']) && $responseObj['openid'] != ""){
                $openid = $responseObj['openid'];
                $infoUrl = "https://api.weixin.qq.com/sns/userinfo";
                $postData = array('access_token'=>$responseObj['access_token'],'openid'=>$responseObj['openid']);
                $responseObj = json_decode(curlPost($infoUrl, $postData), true);
                set_cookie("openid",$responseObj['openid']);
            }
component_access_token:是通过微信给第三方10分钟定时发送的信息里ticket,从接口https://api.weixin.qq.com/cgi-bin/component/api_component_token获得。
至此,第三方平台授权应该完成一个比较完整的功能开发。

TOP

发新话题