您的位置:首页 > 移动开发 > 微信开发

[置顶] 微信网页授权获取用户信息--------小白教程(简单)

2016-09-28 22:18 537 查看

微信网页授权获取用户信息--------小白教程(简单)

准备工作。
1.申请微信公众平台测试帐号。
2.phpstudy服务器(百度下载安装)。

一.新建index.php文件接收用户唯一ID,昵称,性别,头像,所在地

<?php
/*网页授权登录*/
header("Content-type:text/html;charset=utf-8");
 $code=$_GET['code'];/*获取openid*/
 $url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=*****&secret=***************&code=".$code."&grant_type=authorization_code ";
 $opendata=json_decode(gettoken($url),TRUE);
// print_r($opendata);

$token=$opendata['access_token'];
$openid=$opendata['openid'];
$userinfo="https://api.weixin.qq.com/sns/userinfo?access_token=".$token."&openid=".$openid."&lang=zh_CN ";
$userdata=json_decode(gettoken($userinfo),TRUE);
// print_r($userdata);
print "<div>".$openid."</div><br />";
print "<div>".$userdata['nickname']."</div><br />";
print "<div>".$userdata['sex']."</div><br />";
print "<div>".$userdata['province']."</div><br />";
print "<div>".$userdata['city']."</div><br />";
print "<img src=".$userdata['headimgurl']."/><br />";

  function gettoken($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22");
    curl_setopt($ch, CURLOPT_ENCODING ,'gzip'); //加入gzip解析
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

?>

说明:

appid和secret为:



2.将index.php部署到phpstudy服务器,部署完之后我的index.php文件地址为:http://192.168.1.100/weixin/index.php

(自行查看phpstudy的部署)

3.设置微信公众测试帐号的配置





4.在微信公众平台测试号里面发送

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

redirect_uri:index.php文件地址(http://192.168.1.100/weixin/index.php)

5.在微信公众测试帐号点击打开该链接就可以授权登录,显示用户信息。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: