php 微信登录 公众号 获取用户信息 微信网页授权
php 微信登录 公众号 获取用户信息 微信网页授权
先自己建立两个文件: index.php 和 getUserInfo.php
index.php
<?php
//scope=snsapi_userinfo实例
$appid=‘‘; //填写你公众号的appid
$redirect_uri = urlencode ( ‘http://fenlei.sun0758.com/WX/getUserInfo.php‘ ); //回调页面 getUserInfo.php 不能写错
$url ="https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
header("Location:".$url);
getUserInfo.php
<?php
header("Content-type: text/html; charset=utf-8");
$appid = ""; //填写你公众号的appid
$secret = ""; //填写你公众号的secret
$code = $_GET["code"];
//第一步:取得openid
$oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";
$oauth2 = getJson($oauth2Url);
//第二步:根据全局access_token和openid查询用户信息
$access_token = $oauth2["access_token"];
$openid = $oauth2[‘openid‘];
$get_user_info_url = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";
$userinfo = getJson($get_user_info_url);
//打印用户信息
print_r($userinfo);
function getJson($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return json_decode($output, true);
}
将网址: http://你的域名/index.php 生成一个二维码!便能制作扫描二维码登录了。
切记
1. https://api.weixin.qq.com/sns/userinfo
2. https://api.weixin.qq.com/cgi-bin/user/info
上面两个api都是获取用户信息的,并且传入的参数也是一样的,第一个是用户网页端,第二个使用公共号关注后获取用户信息,希望以后的小伙伴可以记住。不要跟我犯一样的错误了。
php 微信登录 公众号 获取用户信息 微信网页授权
原文:http://www.cnblogs.com/fan-bk/p/7700114.html