微信登录

来源:互联网 发布:做企业站用哪个cms 编辑:程序博客网 时间:2024/04/28 15:03

js显示二维码:

<script>      var obj = new WxLogin({      id: "显示id",      appid: "---appid---",      scope: "snsapi_login",      redirect_uri: encodeURIComponent("http://回调地址"),      state: Math.ceil(Math.random()*1000),      style: "black",      href: ""});</script>

php显示二维码:

$appid = '---appid---';$redirect_uri = "回调地址";$url = "https://open.weixin.qq.com/connect/qrconnect?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_login&state=1&connect_redirect=1#wechat_redirect";header('location:'.$url);


<?php/*appid               是   公众号的唯一标识redirect_uri        是   授权后重定向的回调链接地址,请使用urlencode对链接进行处理response_type       是   返回类型,请填写codescope               是   应用授权作用域,snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid),snsapi_userinfo                    (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且,即使在未关注的情况下,只要用户授权,也能获取其信息)state               否   重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值#wechat_redirect    是   无论直接打开还是做页面302重定向时候,必须带此参数*/$code = $_GET["code"];$state = $_GET['state'];//换成自己的接口信息$appid = '---appid---';$appsecret = trim('---appsecret---');if (empty($code)) echo '授权失败';//通过code参数,appid,appsecret通过api获取access_token$token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code'; //echo $token_url;$token = json_decode(file_get_contents($token_url));if (isset($token->errcode)) {    echo '<h1>错误:</h1>'.$token->errcode;    echo '<br/><h2>错误信息:</h2>'.$token->errmsg;    exit;}//access_token是调用授权关系接口的调用凭证//access_token有效期(目前为2个小时)较短,可以使用refresh_token进行刷新$access_token_url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.$appid.'&grant_type=refresh_token&refresh_token='.$token->refresh_token;//转成对象$access_token = json_decode(file_get_contents($access_token_url));if (isset($access_token->errcode)) {    echo '<h1>错误:</h1>'.$access_token->errcode;    echo '<br/><h2>错误信息:</h2>'.$access_token->errmsg;    exit;}//获取/刷新 access_token后,进行接口调用,获取用户信息$user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token->access_token.'&openid='.$access_token->openid.'&lang=zh_CN';//转成对象$user_info = json_decode(file_get_contents($user_info_url));if (isset($user_info->errcode)) {    echo '<h1>错误:</h1>'.$user_info->errcode;    echo '<br/><h2>错误信息:</h2>'.$user_info->errmsg;    exit;}//打印用户信息echo '<pre>';print_r($user_info);echo '</pre>';

获得了开放平台认证的企业,可以绑定10个手机应用、10个网站、10个公众号,他们对应的用户信息也可以互联互通。
强烈提示:对于开发团队而言,在接入微信登录之初,往往会忽视用户的UnionID,只记录了openid。这会导致后期账号打通时候,需要用户重新授权,运营成本增高,遇到此类问题的团队不在少数。

原创粉丝点击