在平時項目開發過程當中,除了註冊本網站帳號進行登陸以外,還能夠調用第三方接口進行登陸網站。這裏以微博登陸爲例。微博登陸包括身份認證、用戶關係以及內容傳播。容許用戶使用微博賬號登陸訪問第三方網站,分享內容,同步信息。php
一、首先須要引導須要受權的用戶到以下地址:html
https://api.weibo.com/oauth2/authorize?client_id=YOUR_CLIENT_ID&response_type=code&redirect_uri=YOUR_REGISTERED_REDIRECT_URI
若是用戶贊成受權,頁面跳轉至 YOUR_REGISTERED_REDIRECT_URI/?code=CODE:web
二、接下來要根據上面獲得的code來換取Access Token:json
https://api.weibo.com/oauth2/access_token?client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET&grant_type=authorization_code&redirect_uri=YOUR_REGISTERED_REDIRECT_URI&code=CODE
返回值:api
JSON安全
{ "access_token": "SlAV32hkKG", "remind_in": 3600, "expires_in": 3600 }
三、最後,使用得到的OAuth2.0 Access Token調用API,獲取用戶身份,完成用戶的登陸。app
話很少說,直接上代碼:curl
爲了方便,咱們先將get和post封裝到application下的common.php中:
應用公共文件common.php:post
function get( $url, $_header = NULL ) { $curl = curl_init(); //curl_setopt ( $curl, CURLOPT_SAFE_UPLOAD, false); if( stripos($url, 'https://') !==FALSE ) { curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); } curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); if ( $_header != NULL ) { curl_setopt($curl, CURLOPT_HTTPHEADER, $_header); } $ret = curl_exec($curl); $info = curl_getinfo($curl); curl_close($curl); if( intval( $info["http_code"] ) == 200 ) { return $ret; } return false; } /* * post method */ function post( $url, $param ) { $oCurl = curl_init (); curl_setopt ( $oCurl, CURLOPT_SAFE_UPLOAD, false); if (stripos ( $url, "https://" ) !== FALSE) { curl_setopt ( $oCurl, CURLOPT_SSL_VERIFYPEER, FALSE ); curl_setopt ( $oCurl, CURLOPT_SSL_VERIFYHOST, false ); } curl_setopt ( $oCurl, CURLOPT_URL, $url ); curl_setopt ( $oCurl, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt ( $oCurl, CURLOPT_POST, true ); curl_setopt ( $oCurl, CURLOPT_POSTFIELDS, $param ); $sContent = curl_exec ( $oCurl ); $aStatus = curl_getinfo ( $oCurl ); curl_close ( $oCurl ); if (intval ( $aStatus ["http_code"] ) == 200) { return $sContent; } else { return false; } }
控制器處理代碼Login.php:網站
class Login extends \think\Controller { public function index() { $key = "****"; $redirect_uri = "***微博應用安全域名***/?backurl=***項目本地域名***/home/login/webLogin?"; //受權後將頁面重定向到本地項目 $redirect_uri = urlencode($redirect_uri); $wb_url = "https://api.weibo.com/oauth2/authorize?client_id={$key}&response_type=code&redirect_uri={$redirect_uri}"; $this -> assign('wb_url',$wb_url); return view('login'); } public function webLogin(){ $key = "*****"; //接收code值 $code = input('get.code'); //換取Access Token: post方式請求 替換參數: client_id, client_secret,redirect_uri, code $secret = "********"; $redirect_uri = "********"; $url = "https://api.weibo.com/oauth2/access_token?client_id={$key}&client_secret={$secret}&grant_type=authorization_code&redirect_uri={$redirect_uri}&code={$code}"; $token = post($url, array()); $token = json_decode($token, true); //獲取用戶信息 : get方法,替換參數: access_token, uid $url = "https://api.weibo.com/2/users/show.json?access_token={$token['access_token']}&uid={$token['uid']}"; $info = get($url); if($info){ echo "<p>登陸成功</p>"; } } }
模板代碼login.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>微博登陸</title> </head> <body> <a href="{$wb_url}">點擊這裏進行微博登陸</a> </body> </html>
效果圖: