微信网页授权登录

user_info模式授权


新建index.php

<?php
$appid='你的Appid';
$redirect_uri=urlencode('http://act.eastjinfu.com/getOpenid.php');
$scope='snsapi_userinfo';//获取的方式;
$url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=".$scope."&state=1&connect_redirect=1#wechat_redirect";
header("Location:".$url);

新建getOpenid.php

<?php
//获取用户openid
$appid="你的APPID";
$appsecret="你的APPsecret";
$code=$_GET['code'];

    $url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code";
        $token = json_decode(file_get_contents($url));
$openid= $token->openid;
$access_token = $token->access_token;
    
$get_user_info_url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh\_CN";
$userinfo = json_decode(file_get_contents($get_user_info_url));
print_r($userinfo);




user_base模式  并判断是否关注了公众号

新建index.php

<?php
$appid = "你的APPID";
$appsecret="你的APPsecret";
$redirect_uri=urlencode('http://act.eastjinfu.com/getOpenid.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);


新建getOpenid.php

<?php
//获取用户openid
$appid="你的APPID";
$appsecret="你的APPsecret";
$code=$_GET['code'];
$url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code";
$token = json_decode(file_get_contents($url));
//获取access_token   由于微信网页授权的access_token和基本的access_token不一样  此处获取的是基本的access_token
$url_access_token ="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
$access_token = json_decode(file_get_contents($url_access_token));
$user_url="https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token->access_token."&openid=".$token->openid."&lang=zh_CN";
$user = json_decode(file_get_contents($user_url));
print_r($user);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
微信网页授权登录demo是一个示例程序,用于展示如何在网站或应用中使用微信登录功能。 首先,我们需要在微信公众平台上申请一个开发者账号,并创建一个应用。在应用设置中,我们需要配置网页授权的回调地址。 接下来,我们可以开始编写示例程序。首先,我们需要引入微信提供的SDK,这个SDK包含了一些必要的函数和方法。 在程序中,我们首先需要处理用户点击登录按钮的事件。当用户点击登录按钮时,我们需要调用微信的JSAPI,发起授权请求。我们可以通过调用微信提供的接口,获取到一个临时的code。 接着,我们需要通过这个code,向微信服务器发送请求,获取用户的access_token和openid。这些信息将用于后续的用户信息获取。 一般情况下,我们会将这些信息存储在服务器端或数据库中,以供后续使用。在示例程序中,我们可以简单地将这些信息暂存到本地的session中。 最后,我们可以根据用户的access_token和openid,通过微信提供的接口,获取到用户的基本信息,如昵称、头像等。 至此,我们就完成了一个简单的微信网页授权登录demo。当用户在我们的网站或应用中点击登录按钮后,会跳转到微信授权页面,用户同意授权后,我们可以获取到用户的基本信息,实现登录功能。 需要注意的是,我们在开发过程中还需要处理一些异常情况,如用户未同意授权授权过期等。另外,由于微信的接口是需要网络访问的,因此我们需要确保程序可以正常访问网络。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值