在這個行動端的時代,微信公眾號也逐漸成為人們獲取資訊的一種管道,也是商家發展潛在客戶的一種手段,因此,許多程式設計師轉戰微信開發,那麼今天,我們就來為大家介紹一下微信公眾號開發中的網頁授權到底要怎麼達成。
在這之前先給大家一個我自訂的請求介面的函數,在下面的範例程式碼中請求介面用的都是這個函數
這個函數的作用是,想介面發起請求,傳遞參數並回傳介面回傳的資料
(這個裡面的程式碼就不做多解釋了,如果大家想要了解可以去看一下php curl函數總結)
//自定义请求接口函数,$data为空时发起get请求,$data有值时发情post请求 function http_url($url,$data=null){ $ch = curl_init(); curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0); curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE); if(!empty($data)){ curl_setopt($ch,CURLOPT_POST,1); curl_setopt($ch,CURLOPT_POSTFIELDS,$data); } $res = curl_exec($ch); if(curl_errno($ch)){ echo "error:".curl_error($ch); exit; } curl_close($ch); return $res; }
(文中所使用的介面為騰訊官方提供,大家可以參考一下微信公眾平台的開發者文件)
一、首先我們需要設定我們的公眾號碼
#1、在微信公眾號請求使用者網頁授權之前,開發者需要先到公眾平台官網中的「開發- 介面權限- 網頁服務- 網頁帳號- 網頁授權取得使用者基本資訊」的設定選項中,修改授權回呼網域名稱。請注意,這裡填寫的是網域名稱(是字串),而不是URL,所以請勿加http:// 等協定頭;
2、授權回呼網域設定規範為全域名,例如需要網頁授權的網域為:www.qq.com,設定日後此網域下方的頁面http://www.qq.com/music.html 、 http://www.qq.com/login .html 都可以進行OAuth2.0鑑權。但http://pay.qq.com 、 http://music.qq.com 、 http://qq.com無法進行OAuth2.0鑑權
3、如果公有號登入授權給了第三方開發者來進行管理,不必做任何設置,由第三方代替公眾號實現網頁授權即可
#二、使用者同意授權,取得code
function Get_Code() //获取code { //构造请求地址 $code_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=微信公众号appid&redirect_uri=请求功后回调地址&response_type=code&scope=snsapi_userinfo&state=STATE #wechat_redirect"; //跳转到请求地址,应为本省设置了回调地址,所以不需要使用file_get_content()来请求接口。 header("location:" . $code_url); exit; }
三、通個取得到的code來或缺少access_token和openid
/** * 通过获取到的code来获取access_token和openid * $code为获取到的code * 接口的参数注意换成自己的,如appid和secret */ function GetAccess_Token($code) { $get_access_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=appid&secret=secret&code=$code&grant_type=authorization_code"; $res = http_url($get_access_token_url); return json_decode($res, true); }
介面:https://api.weixin.qq.com/sns/auth?access_token=ACCESS_TOKEN&openid=OPENID
###五、如果失效,刷新access_token###############介面:### https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=APPID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN###############六、取得使用者資訊############介面:###https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN##### #/** * 获取用户基本信息 * */ function Get_User_Info($access_token, $openid){ $get_user_info = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN"; $res = http_url($get_user_info); return json_decode($res, true); }
获取到用户信息数据:
{ "openid":" OPENID", " nickname": NICKNAME, "sex":"1", "province":"PROVINCE" "city":"CITY", "country":"COUNTRY", "headimgurl": "http://thirdwx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/46", "privilege":[ "PRIVILEGE1" "PRIVILEGE2" ], "unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL" }
下面上完整代码:
<?php //跳转第三方页面,获取用户基本信息 // 这是请求页面也是code的回调页面 session_start(); //启动session if (isset($_GET['code'])) { //判断是否有code传过来,如果没有调用函数请求code $res = GetAccess_Token($_GET['code']); //使用code获取access_token和openid if (CkeckAccessToken($res['access_token'], $res['openid']) == 0) { //判断access_token是否有效,如果无效获取新的access_token $res = GetRefresh_Token($res['refresh_token']); //或缺新的access_token } $userinfo = Get_User_Info($res['access_token'], $res['openid']); //获取用户信息 $_SESSION['userinfo'] = $userinfo; //将用户信息存入session中 $next_url = 'http://web/index.php'; //下一个页面地址 header("location:" . $next_url); //获取到信息后跳转到其他页面 exit; } else { //获取code Get_Code(); } function Get_Code() //获取code{ $code_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=appid&redirect_uri=回调地址&response_type=code&scope=snsapi_userinfo&state=STATE #wechat_redirect"; header("location:" . $code_url); exit; } /** * 通过获取到的code来获取access_token和openid * */ function GetAccess_Token($code){ $get_access_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=appid&secret=secret&code=$code&grant_type=authorization_code"; $res = http_url($get_access_token_url); return json_decode($res, true); } /** * 检查access_token是否有效 * */ function CkeckAccessToken($access_token, $openid){ $check_url = "https://api.weixin.qq.com/sns/auth?access_token=$access_token&openid=$openid"; $res = http_url($check_url); $result = json_decode($res, true); if (isset($result['errmsg']) && $result['errmsg'] == 1) { return 1; //access_token有效 } else { return 0; //access_token无效 } } /** * 如果获取到的access_token无效,通过refresh_token来刷新access_token */ function GetRefresh_Token($refresh_token){ $get_refresh_token_url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=appid&grant_type=refresh_token&refresh_token=$refresh_token"; $res = http_url($get_refresh_token_url); return json_decode($res, true); } /** * 获取用户基本信息 * */ function Get_User_Info($access_token, $openid){ $get_user_info = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN"; $res = http_url($get_user_info); return json_decode($res, true);} //自定义请求接口函数,$data为空时发起get请求,$data有值时发起post请求 function http_url($url,$data=null){ $ch = curl_init(); curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0); curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE); if(!empty($data)){ curl_setopt($ch,CURLOPT_POST,1); curl_setopt($ch,CURLOPT_POSTFIELDS,$data); } $res = curl_exec($ch); if(curl_errno($ch)){ echo "error:".curl_error($ch); exit; } curl_close($ch); return $res; }
以上是微信公眾號網頁授權詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

WebStorm Mac版
好用的JavaScript開發工具

Dreamweaver CS6
視覺化網頁開發工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。