日韩精品成人无码专区免费-国产99久久久久久免费看-国产精品丝袜久久久久久不卡-国产精品无码一区二区三区

基于PHP微信網頁獲取用戶信息的實例分析

發布時間:2025-01-03 點擊:120
很多用戶在開發微信版網頁的時候,需要獲取用戶的基本信息,比如國家,省,市,昵稱等,我們接下來基于php語言基礎詳細分析一下如何成功獲取。
相關視頻推薦:php編程從入門到精通
必要條件:
1)公眾號認證
2)有網頁授權獲取用戶基本信息的權限接口
注意:最近有朋友說:在公眾平臺申請的測試號,會出現無法取到用戶信息。換到認證的公眾賬號就正常了!
如果您也遇到這個問題,可以試試在認證的公眾賬號里測試一下! 感謝大家的支持!
填寫授權回調頁面的域名
登錄公眾平臺–>開發者中心–>接口權限表
找到 網頁授權獲取用戶基本信息 然后修改–>填寫你的域名.如下:
保存即可!
關于網頁授權的兩種scope的區別說明(官方)
1、以snsapi_base為scope發起的網頁授權,是用來獲取進入頁面的用戶的openid的,并且是靜默授權并自動跳轉到回調頁的。用戶感知的就是直接進入了回調頁(往往是業務頁面)
2、以snsapi_userinfo為scope發起的網頁授權,是用來獲取用戶的基本信息的。但這種授權需要用戶手動同意,并且由于用戶同意過,所以無須關注,就可在授權后獲取該用戶的基本信息。
3、用戶管理類接口中的“獲取用戶基本信息接口”,是在用戶和公眾號產生消息交互或關注后事件推送后,才能根據用戶openid來獲取用戶基本信息。這個接口,包括其他微信接口,都是需要該用戶(即openid)關注了公眾號后,才能調用成功的。
因為scope有兩中模式,所以下面分開解說:
scope為snsapi_base 那么用戶必須是關注了公眾號才能取得信息
先自己建立兩個文件: index.php 和 getuserinfo.php
代碼實例
index.php如下:
//scope=snsapi_base 實例$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); //第三步:根據全局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);}scope為snsapi_userinfo 用戶不用關注公眾號,也能取到信息,但是會有一個界面讓用戶去點擊確認!相當于一個登錄授權吧!
代碼實例
index.php如下:
//scope=snsapi_userinfo實例$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);//第二步:根據全局access_token和openid查詢用戶信息$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);//打印用戶信息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);}測試步驟:
創建index.php和getuserinfo.php兩個文件后
先測試:scope為snsapi_base
1)先關注公眾賬號
2)將網址: http://你的域名/index.php 生成一個二維碼!
3)用微信掃一掃
再測試:scope為snsapi_userinfo
1)替換代碼
2)取消關注當前公眾號.
3)然后用微信掃一掃,剛剛你生成的二維碼.
相關學習推薦:php編程(視頻)

域名未來投資趨勢:創意域名交易
阿里云一個服務器多個域名備案
服務器被CC攻擊了怎么辦
中文網址在哪里注冊
谷歌瀏覽器scholarscope如何安裝 谷歌瀏覽器安裝scholarscope的方法
二手電商市場又起硝煙 京東重啟拍拍二手品牌
免費網址域名使用陷阱和突出問題
秒換ip云服務器租用