Inhaltsverzeichnis
错误:
错误信息:
Heim php教程 php手册 微信公众平台实现微信网页登陆授权开发

微信公众平台实现微信网页登陆授权开发

May 26, 2016 am 08:19 AM

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

文件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');
Nach dem Login kopieren

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

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;
Nach dem Login kopieren
'; ?>

参数 描述

openid 用户的唯一标识

nickname 用户昵称

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

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

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

country 国家,如中国为CN

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

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

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

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


文章地址:

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

Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

AI Hentai Generator

AI Hentai Generator

Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

R.E.P.O. Energiekristalle erklärten und was sie tun (gelber Kristall)
4 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Beste grafische Einstellungen
4 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. So reparieren Sie Audio, wenn Sie niemanden hören können
4 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: Wie man alles in Myrise freischaltet
1 Monate vor By 尊渡假赌尊渡假赌尊渡假赌

Heiße Werkzeuge

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)