cari
Rumahapplet WeChatpembangunan WeChat微信公众号-获取用户信息(网页授权获取)实现步骤

根据微信公众号开发官方文档:

获取用户信息步骤如下:

1 第一步:用户同意授权,获取code
2 第二步:通过code换取网页授权access_token
3 第三步:刷新access_token(如果需要)
4 第四步:拉取用户信息(需scope为 snsapi_userinfo)

1 获取code

在确保微信公众账号拥有授权作用域(scope参数)的权限的前提下(服务号获得高级接口后,默认拥有scope参数中的snsapi_base和snsapi_userinfo),引导关注者打开如下页面:

https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect

若提示“该链接无法访问”,请检查参数是否填写错误,是否拥有scope参数对应的授权作用域权限。

尤其注意:由于授权操作安全等级较高,所以在发起授权请求时,微信会对授权链接做正则强匹配校验,如果链接的参数顺序不对,授权页面将无法正常访问

其中:

AppID - 公众号的唯一标识
REDIRECT_URI - 跳转url
SCOPE - 值为snsapi_base(不弹出授权页面,直接跳转,只能获取用户openid) 或snsapi_userinfo (弹 出授权页面,可通过openid拿到昵称、性别、所在地。并且,即使在未关注的情况下,只要用户授权,也能获取其信息)
STATE - 开发者可以自定义填写a-zA-Z0-9的参数值

2 通过code换取网页授权access_token

如果用户同意授权,页面将跳转至 redirect_uri/?code=CODE&state=STATE。
state就是上面的STATE参数原样传过来的

实现代码:   

<code class="hljs php">$code = I(&#39;get.code&#39;);
if (empty($code)) {
   //todo 非微信访问
   exit(&#39;</code>&#39;);
 }else{ //授权后操作 }

    在这里我们就可以得到code用作后续的获取access_token。

获取code后,请求以下链接获取access_token:

 https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code

appid - 公众号的唯一标识
secret - 密钥
code - 上述所返回的code
grant_type - 值为authorization_code

实现代码:

<code class="hljs bash">$url = &#39;https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=&#39; . C(&#39;wechat.AppID&#39;) . &#39;&secret=&#39; . C(&#39;wechat.AppSecret&#39;);
$str = file_get_contents($url);
$str = json_decode($str, true);
$access_token = $str[&#39;access_token&#39;];</code>

这里access_token可以做缓存处理,避免造成频繁获取
实现代码,以TP框架为例:

<code class="hljs php">$access_token = S(&#39;access_token&#39;);
if (empty($access_token)) {
  $url = &#39;https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=&#39; . C(&#39;wechat.AppID&#39;) . &#39;&secret=&#39; . C(&#39;wechat.AppSecret&#39;);
  $str = file_get_contents($url);
  $str = json_decode($str, true);
  $access_token = $str[&#39;access_token&#39;];
  S(&#39;access_token&#39;, $access_token, 3600);
}</code>

在获取access_token后,也会一并返回openid(用户唯一标识),微信官方文档的解释是:用户唯一标识,请注意,在未关注公众号时,用户访问公众号的网页,也会产生一个用户和公众号唯一的OpenID

openid是唯一标识微信用户的,如果用户不是第一次登陆,可以在得到openid后查询数据库是否有绑定此openid的用户,之后就无需重新获取用户数据,直接获取的数据库user_id设置session,直接登陆访问

3 第三步忽略,只在需要的时间重新获取access_token而已

4 拉取用户信息(需scope为 snsapi_userinfo)

在数据库无此微信号用户的绑定下,就相当于用户首次访问登陆,则通过第四步来获取用户信息(在用户授权情况下,网页授权作用域为snsapi_userinfo,则此时开发者可以通过access_token和openid拉取用户信息了),然后后台创建user并绑定此微

信用户(通过openid)

请求方法

http:GET(请使用https协议) https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN

access_token - 上述所获取的access_token
openid - 公众号唯一标识

实现代码:

<code class="hljs php">$access_token = S(&#39;access_token&#39;);
if (empty($access_token)) {
  $url = &#39;https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=&#39; . C(&#39;wechat.AppID&#39;) . &#39;&secret=&#39; . C(&#39;wechat.AppSecret&#39;);
  $str = file_get_contents($url);
  $str = json_decode($str, true);
  $access_token = $str[&#39;access_token&#39;];
  S(&#39;access_token&#39;, $access_token, 3600);
}</code>

以上就是微信公众号获取用户信息的具体步骤。

Kenyataan
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn

Alat AI Hot

Undresser.AI Undress

Undresser.AI Undress

Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover

AI Clothes Remover

Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool

Undress AI Tool

Gambar buka pakaian secara percuma

Clothoff.io

Clothoff.io

Penyingkiran pakaian AI

Video Face Swap

Video Face Swap

Tukar muka dalam mana-mana video dengan mudah menggunakan alat tukar muka AI percuma kami!

Alat panas

Hantar Studio 13.0.1

Hantar Studio 13.0.1

Persekitaran pembangunan bersepadu PHP yang berkuasa

SublimeText3 versi Cina

SublimeText3 versi Cina

Versi Cina, sangat mudah digunakan

MinGW - GNU Minimalis untuk Windows

MinGW - GNU Minimalis untuk Windows

Projek ini dalam proses untuk dipindahkan ke osdn.net/projects/mingw, anda boleh terus mengikuti kami di sana. MinGW: Port Windows asli bagi GNU Compiler Collection (GCC), perpustakaan import yang boleh diedarkan secara bebas dan fail pengepala untuk membina aplikasi Windows asli termasuk sambungan kepada masa jalan MSVC untuk menyokong fungsi C99. Semua perisian MinGW boleh dijalankan pada platform Windows 64-bit.

PhpStorm versi Mac

PhpStorm versi Mac

Alat pembangunan bersepadu PHP profesional terkini (2018.2.1).

SublimeText3 versi Mac

SublimeText3 versi Mac

Perisian penyuntingan kod peringkat Tuhan (SublimeText3)