PHP微信授权登录信息

<strong>文件1:index.php</strong>

//换成自己的接口信息

$appid = ‘XXXXX’;

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

<strong>参数说明:</strong>

&nbsp;
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="120">
<div align="center"><strong>参数</strong></div></td>
<td width="120">
<div align="center"><strong>是否必须</strong></div></td>
<td>
<div align="center"><strong>说明</strong></div></td>
</tr>
<tr>
<td>appid</td>
<td>是</td>
<td>公众号的唯一标识</td>
</tr>
<tr>
<td>redirect_uri</td>
<td>是</td>
<td>授权后重定向的回调链接地址,请使用urlencode对链接进行处理</td>
</tr>
<tr>
<td>response_type</td>
<td>是</td>
<td>返回类型,请填写code</td>
</tr>
<tr>
<td>scope</td>
<td>是</td>
<td>应用授权作用域,snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid),snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且,<strong>即使在未关注的情况下,只要用户授权,也能获取其信息</strong>)</td>
</tr>
<tr>
<td>state</td>
<td>否</td>
<td>重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值</td>
</tr>
<tr>
<td>#wechat_redirect</td>
<td>是</td>
<td>无论直接打开还是做页面302重定向时候,必须带此参数</td>
</tr>
</tbody>
</table>
<strong>文件二:oauth.php</strong>

&lt;?php
$code = $_GET[‘code’];
$state = $_GET[‘state’];
//换成自己的接口信息
$appid = ‘XXXXX’;
$appsecret = ‘XXXXX’;
if (empty($code)) $this-&gt;error(‘授权失败’);
$token_url = ‘https://api.weixin.qq.com/sns/oauth2/access_token?appid=’.$appid.’&amp;secret=’.$appsecret.’&amp;code=’.$code.’&amp;grant_type=authorization_code’;
$token = json_decode(file_get_contents($token_url));
if (isset($token-&gt;errcode)) {
echo ‘&lt;h1&gt;错误:&lt;/h1&gt;’.$token-&gt;errcode;
echo ‘&lt;br/&gt;&lt;h2&gt;错误信息:&lt;/h2&gt;’.$token-&gt;errmsg;
exit;
}
$access_token_url = ‘https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=’.$appid.’&amp;grant_type=refresh_token&amp;refresh_token=’.$token-&gt;refresh_token;
//转成对象
$access_token = json_decode(file_get_contents($access_token_url));
if (isset($access_token-&gt;errcode)) {
echo ‘&lt;h1&gt;错误:&lt;/h1&gt;’.$access_token-&gt;errcode;
echo ‘&lt;br/&gt;&lt;h2&gt;错误信息:&lt;/h2&gt;’.$access_token-&gt;errmsg;
exit;
}
$user_info_url = ‘https://api.weixin.qq.com/sns/userinfo?access_token=’.$access_token-&gt;access_token.’&amp;openid=’.$access_token-&gt;openid.’&amp;lang=zh_CN’;
//转成对象
$user_info = json_decode(file_get_contents($user_info_url));
if (isset($user_info-&gt;errcode)) {
echo ‘&lt;h1&gt;错误:&lt;/h1&gt;’.$user_info-&gt;errcode;
echo ‘&lt;br/&gt;&lt;h2&gt;错误信息:&lt;/h2&gt;’.$user_info-&gt;errmsg;
exit;
}
//打印用户信息
echo ‘&lt;pre&gt;’;
print_r($user_info);
echo ‘&lt;/pre&gt;’;
?&gt;
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="180">
<div align="center"><strong>参数</strong></div></td>
<td width="470">
<div align="center"><strong>描述</strong></div></td>
</tr>
<tr>
<td>openid</td>
<td>用户的唯一标识</td>
</tr>
<tr>
<td>nickname</td>
<td>用户昵称</td>
</tr>
<tr>
<td>sex</td>
<td>用户的性别,值为1时是男性,值为2时是女性,值为0时是未知</td>
</tr>
<tr>
<td>province</td>
<td>用户个人资料填写的省份</td>
</tr>
<tr>
<td>city</td>
<td>普通用户个人资料填写的城市</td>
</tr>
<tr>
<td>country</td>
<td>国家,如中国为CN</td>
</tr>
<tr>
<td>headimgurl</td>
<td>用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空</td>
</tr>
<tr>
<td>privilege</td>
<td>用户特权信息,json 数组,如微信沃卡用户为(chinaunicom)</td>
</tr>
<tr>
<td>unionid</td>
<td>只有在用户将公众号绑定到微信开放平台帐号后,才会出现该字段。详见:获取用户个人信息(UnionID机制)</td>
</tr>
</tbody>
</table>
&nbsp;

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