欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

實(shí)例分析基于PHP微信網(wǎng)頁(yè)獲取用戶(hù)信息

 更新時(shí)間:2017年11月24日 09:56:32   投稿:laozhang  
本篇內(nèi)容主要給大家詳細(xì)分析了用PHP制作微信網(wǎng)頁(yè)來(lái)獲取用戶(hù)基本信息的過(guò)程,以及步驟講解。

很多用戶(hù)在開(kāi)發(fā)微信版網(wǎng)頁(yè)的時(shí)候,需要獲取用戶(hù)的基本信息,比如國(guó)家,省,市,昵稱(chēng)等,我們接下來(lái)基于PHP語(yǔ)言基礎(chǔ)詳細(xì)分析一下如何成功獲取。

必要條件:

1)公眾號(hào)認(rèn)證

2)有網(wǎng)頁(yè)授權(quán)獲取用戶(hù)基本信息的權(quán)限接口

注意:最近有朋友說(shuō):在公眾平臺(tái)申請(qǐng)的測(cè)試號(hào),會(huì)出現(xiàn)無(wú)法取到用戶(hù)信息。換到認(rèn)證的公眾賬號(hào)就正常了!

如果您也遇到這個(gè)問(wèn)題,可以試試在認(rèn)證的公眾賬號(hào)里測(cè)試一下! 感謝大家的支持!

填寫(xiě)授權(quán)回調(diào)頁(yè)面的域名

登錄公眾平臺(tái)-->開(kāi)發(fā)者中心-->接口權(quán)限表

找到 網(wǎng)頁(yè)授權(quán)獲取用戶(hù)基本信息 然后修改-->填寫(xiě)你的域名.如下:

保存即可!

關(guān)于網(wǎng)頁(yè)授權(quán)的兩種scope的區(qū)別說(shuō)明(官方)

1、以snsapi_base為scope發(fā)起的網(wǎng)頁(yè)授權(quán),是用來(lái)獲取進(jìn)入頁(yè)面的用戶(hù)的openid的,并且是靜默授權(quán)并自動(dòng)跳轉(zhuǎn)到回調(diào)頁(yè)的。用戶(hù)感知的就是直接進(jìn)入了回調(diào)頁(yè)(往往是業(yè)務(wù)頁(yè)面)

2、以snsapi_userinfo為scope發(fā)起的網(wǎng)頁(yè)授權(quán),是用來(lái)獲取用戶(hù)的基本信息的。但這種授權(quán)需要用戶(hù)手動(dòng)同意,并且由于用戶(hù)同意過(guò),所以無(wú)須關(guān)注,就可在授權(quán)后獲取該用戶(hù)的基本信息。

3、用戶(hù)管理類(lèi)接口中的“獲取用戶(hù)基本信息接口”,是在用戶(hù)和公眾號(hào)產(chǎn)生消息交互或關(guān)注后事件推送后,才能根據(jù)用戶(hù)OpenID來(lái)獲取用戶(hù)基本信息。這個(gè)接口,包括其他微信接口,都是需要該用戶(hù)(即openid)關(guān)注了公眾號(hào)后,才能調(diào)用成功的。

因?yàn)閟cope有兩中模式,所以下面分開(kāi)解說(shuō):

scope為snsapi_base 那么用戶(hù)必須是關(guān)注了公眾號(hào)才能取得信息

先自己建立兩個(gè)文件: index.php 和 getUserInfo.php

代碼實(shí)例

index.php如下:

//scope=snsapi_base 實(shí)例
$appid='你的AppId';
$redirect_uri = urlencode ( 'http://你的域名/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如下:

$appid = "你的AppId";
$secret = "你的AppSecret";
$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);
 
//第三步:根據(jù)全局access_token和openid查詢(xún)用戶(hù)信息
$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);
//打印用戶(hù)信息
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);
}

scope為snsapi_userinfo 用戶(hù)不用關(guān)注公眾號(hào),也能取到信息,但是會(huì)有一個(gè)界面讓用戶(hù)去點(diǎn)擊確認(rèn)!相當(dāng)于一個(gè)登錄授權(quán)吧!

代碼實(shí)例

index.php如下:

//scope=snsapi_userinfo實(shí)例
$appid='你的AppId';
$redirect_uri = urlencode ( 'http://你的域名/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如下:

$appid = "你的AppId";
$secret = "你的AppSecret";
$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);
//第二步:根據(jù)全局access_token和openid查詢(xún)用戶(hù)信息
$access_token = $oauth2["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);
//打印用戶(hù)信息
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);
}

測(cè)試步驟:

創(chuàng)建index.php和getUserInfo.php兩個(gè)文件后

先測(cè)試:scope為snsapi_base

1)先關(guān)注公眾賬號(hào)

2)將網(wǎng)址: http://你的域名/index.php 生成一個(gè)二維碼!

3)用微信掃一掃

再測(cè)試:scope為snsapi_userinfo

1)替換代碼

2)取消關(guān)注當(dāng)前公眾號(hào).

3)然后用微信掃一掃,剛剛你生成的二維碼.

相關(guān)文章

最新評(píng)論