cari
Rumahphp教程php手册微信公众平台实现微信网页登陆授权开发

微信公众平台实现微信网页登陆授权开发其实是非常的简单了,因为官方的参考程序了,下面小编就看了一站长根据官方参考做的一个网页登陆授权例子,大家可看看.

文件1:index.php

//换成自己的接口信息 

$appid = 'XXXXX'; 
header('location:https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri=127.0.0.1/oauth.php&response_type=code&scope=snsapi_userinfo&state=123&connect_redirect=1#wechat_redirect');

参数说明:参数是否必须说明

appid 是 公众号的唯一标识

redirect_uri 是 授权后重定向的回调链接地址,请使用urlencode对链接进行处理

response_type 是 返回类型,请填写code

scope 是 应用授权作用域,snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid),snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且,即使在未关注的情况下,只要用户授权,也能获取其信息)

state 否 重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值

#wechat_redirect 是 无论直接打开还是做页面302重定向时候,必须带此参数

文件二:oauth.php,代码如下:

<?php 
$code = $_GET[&#39;code&#39;]; 
$state = $_GET[&#39;state&#39;]; 
//换成自己的接口信息 
$appid = &#39;XXXXX&#39;; 
$appsecret = &#39;XXXXX&#39;; 
if (emptyempty($code)) $this->error(&#39;授权失败&#39;); 
$token_url = &#39;https://api.weixin.qq.com/sns/oauth2/access_token?appid=&#39;.$appid.&#39;&secret=&#39;.$appsecret.&#39;&code=&#39;.$code.&#39;&grant_type=authorization_code&#39;; 
$token = json_decode(file_get_contents($token_url)); 
if (isset($token->errcode)) { 
   echo &#39;<h1 id="错误">错误:</h1>&#39;.$token->errcode; 
   echo &#39;<br/><h2 id="错误信息">错误信息:</h2>&#39;.$token->errmsg; 
   exit; 
} 
$access_token_url = &#39;https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=&#39;.$appid.&#39;&grant_type=refresh_token&refresh_token=&#39;.$token->refresh_token; 
//转成对象 
$access_token = json_decode(file_get_contents($access_token_url)); 
if (isset($access_token->errcode)) { 
   echo &#39;<h1 id="错误">错误:</h1>&#39;.$access_token->errcode; 
   echo &#39;<br/><h2 id="错误信息">错误信息:</h2>&#39;.$access_token->errmsg; 
   exit; 
} 
$user_info_url = &#39;https://api.weixin.qq.com/sns/userinfo?access_token=&#39;.$access_token->access_token.&#39;&openid=&#39;.$access_token->openid.&#39;&lang=zh_CN&#39;;   
//转成对象 
$user_info = json_decode(file_get_contents($user_info_url)); 
if (isset($user_info->errcode)) { 
   echo &#39;<h1 id="错误">错误:</h1>&#39;.$user_info->errcode; 
   echo &#39;<br/><h2 id="错误信息">错误信息:</h2>&#39;.$user_info->errmsg; 
   exit; 
} 
//打印用户信息 
echo &#39;<pre class="brush:php;toolbar:false">&#39;; 
print_r($user_info); 
echo &#39;
'; ?>

参数 描述

openid 用户的唯一标识

nickname 用户昵称

sex 用户的性别,值为1时是男性,值为2时是女性,值为0时是未知

province 用户个人资料填写的省份

city 普通用户个人资料填写的城市

country 国家,如中国为CN

headimgurl 用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空.

privilege 用户特权信息,json 数组,如微信沃卡用户为(chinaunicom)

unionid 只有在用户将公众号绑定到微信开放平台帐号后,才会出现该字段。详见:获取用户个人信息(UnionID机制)

到此网页登陆授权开发功能就作完了,如果想要获取用户基本信息我们需要看另一个例子,在官方有说明大家可自行搜索哦.


文章地址:

转载随意^^请带上本文地址!

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

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.

SublimeText3 versi Inggeris

SublimeText3 versi Inggeris

Disyorkan: Versi Win, menyokong gesaan kod!

SublimeText3 Linux versi baharu

SublimeText3 Linux versi baharu

SublimeText3 Linux versi terkini

SublimeText3 versi Mac

SublimeText3 versi Mac

Perisian penyuntingan kod peringkat Tuhan (SublimeText3)

Muat turun versi mac editor Atom

Muat turun versi mac editor Atom

Editor sumber terbuka yang paling popular