在PHP微信網頁裡如何擷取使用者資訊

來源:互聯網
上載者:User
很多使用者在開發版網頁的時候,需要擷取使用者的基本資料,比如國家,省,市,暱稱等,我們接下來基於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中文網其它相關文章!

相關閱讀:

PHP如何解決網站大流量與高並發

AJAX的常用文法是什麼

AJAX原理與CORS跨域的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.