在平時(shí)項(xiàng)目開發(fā)過程中,除了注冊(cè)本網(wǎng)站賬號(hào)進(jìn)行登錄之外,還可以調(diào)用第三方接口進(jìn)行登錄網(wǎng)站。這里以微博登錄為例。微博登錄包括身份認(rèn)證、用戶關(guān)系以及內(nèi)容傳播。允許用戶使用微博帳號(hào)登錄訪問第三方網(wǎng)站,分享內(nèi)容,同步信息。
1、首先需要引導(dǎo)需要授權(quán)的用戶到如下地址:
https://api.weibo.com/oauth2/authorize?client_id=YOUR_CLIENT_IDresponse_type=coderedirect_uri=YOUR_REGISTERED_REDIRECT_URI
如果用戶同意授權(quán),頁面跳轉(zhuǎn)至 YOUR_REGISTERED_REDIRECT_URI/?code=CODE:
2、接下來要根據(jù)上面得到的code來換取Access Token:
https://api.weibo.com/oauth2/access_token?client_id=YOUR_CLIENT_IDclient_secret=YOUR_CLIENT_SECRETgrant_type=authorization_coderedirect_uri=YOUR_REGISTERED_REDIRECT_URIcode=CODE
返回值:
JSON
{
"access_token": "SlAV32hkKG",
"remind_in": 3600,
"expires_in": 3600
}
3、最后,使用獲得的OAuth2.0 Access Token調(diào)用API,獲取用戶身份,完成用戶的登錄。
話不多說,直接上代碼:
為了方便,我們先將get和post封裝到application下的common.php中:
應(yīng)用公共文件common.php:
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 = "***微博應(yīng)用安全域名***/?backurl=***項(xiàng)目本地域名***/home/login/webLogin?";
//授權(quán)后將頁面重定向到本地項(xiàng)目
$redirect_uri = urlencode($redirect_uri);
$wb_url = "https://api.weibo.com/oauth2/authorize?client_id={$key}response_type=coderedirect_uri={$redirect_uri}";
$this -> assign('wb_url',$wb_url);
return view('login');
}
public function webLogin(){
$key = "*****";
//接收code值
$code = input('get.code');
//換取Access Token: post方式請(qǐng)求 替換參數(shù): 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_coderedirect_uri={$redirect_uri}code={$code}";
$token = post($url, array());
$token = json_decode($token, true);
//獲取用戶信息 : get方法,替換參數(shù): 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}" rel="external nofollow" >點(diǎn)擊這里進(jìn)行微博登錄/a>
/body>
/html>
效果圖:
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:- PHP實(shí)現(xiàn)發(fā)送微博消息功能完整示例
- redis+php實(shí)現(xiàn)微博(三)微博列表功能詳解
- redis+php實(shí)現(xiàn)微博(二)發(fā)布與關(guān)注功能詳解
- redis+php實(shí)現(xiàn)微博(一)注冊(cè)與登錄功能詳解
- PHP+redis實(shí)現(xiàn)微博的拉模型案例詳解
- PHP+redis實(shí)現(xiàn)微博的推模型案例分析
- vue+php實(shí)現(xiàn)的微博留言功能示例
- php微信分享到朋友圈、QQ、朋友、微博
- php新浪微博登錄接口用法實(shí)例
- 基于PHP實(shí)現(xiàn)發(fā)微博動(dòng)態(tài)代碼實(shí)例