現在就說說,用微信網頁授權獲取用戶基本信息
首先重要的條件
1公衆號認證
2網頁授權獲取用戶的基本信息(權限接口)
登錄公衆號平臺,開發者中心,接口權限表
網頁授權獲取用戶基本信息,填寫域名
關於網頁授權的兩種scope的區別
1 snsapi_base爲scope發起的網頁授權,是用來獲取進入頁面的用戶openid,並且是靜默授權並自動跳轉到回調頁。
2 snsapi_userinfo爲scope發起的網頁授權,是用來獲取用戶基本信息。但這種授權需要用戶手動同意,並且由於用戶同意過,所以無需關注,就可在授權後獲取用戶基本信息
3 用戶管理類接口中,獲取用戶基本信息,是在用戶和公衆號產生消息交互或關注後事件推送後,才能根據用戶openid來獲取用戶基本信息。這個接口,包括其他微信接口,都是需要該用戶關注公衆號後(openid),才能調用成功的。
建立兩個文件
index.php
<?php
$appid='XXXXXXXX';
$redirect_uri = urlencode ( 'http://www.XXXXX.com/mobile/ceshi/weixin/getUserInfo.php' );
$url ="https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_base&state=1#wechat_redirect";
header("Location:".$url);
?>
getUserInfo.php
<?php
header("Content-type: text/html; charset=utf-8");
$appid = "XXXXXXX";
$secret = "xxxxxxxxxxxxxxxxxxxxxx";
$code = $_GET["code"];
//第一步:取全局access_token
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";
$token = getJson($url);
//第二步:取得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 = $token["access_token"];
$openid = $oauth2['openid'];
$get_user_info_url = "https://api.weixin.qq.com/cgi-bin/user/info?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);
}
?>
如果沒關注的話,獲取不到基本信息,就獲取openid
weixin2文件夾建兩個文件 index.php getUserInfo.php
<?php
$appid='XXXXXXXXXXXXXXXXXXXXX';
$redirect_uri = urlencode ( 'http://www.xxxxxx.com/mobile/ceshi/weixin2/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);
?>
<?php
header("Content-type: text/html; charset=utf-8");
$appid = "XXXXXXXXXXXX";
$secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$code = $_GET["code"];
//print_r($code);exit;
//第一步:取得openid
$oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";
$oauth2 = getJson($oauth2Url);
print_r($oauth2);exit;
//第二步:根據全局access_token和openid查詢用戶信息
$access_token = $oauth2["access_token"];
//print_r($access_token);exit;
$openid = $oauth2['openid'];
//$get_user_info_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN";
$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);
}
?>
即使沒關注也可以獲取全部信息
如果要獲取unionid ,就把微信公衆號綁定到微信開發平臺。
在同一個微信開發平臺,綁定不同的小程序或app,微信公衆號,那麼登錄獲取的unionid都相同