PHP調(diào)用微博接口實(shí)現(xiàn)微博登錄的方法示例
在平時(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_ID&response_type=code&redirect_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_ID&client_secret=YOUR_CLIENT_SECRET&grant_type=authorization_code&redirect_uri=YOUR_REGISTERED_REDIRECT_URI&code=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=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方式請(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_code&redirect_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í)例
相關(guān)文章
Laravel等框架模型關(guān)聯(lián)的可用性淺析
這篇文章主要給大家介紹了關(guān)于Laravel等框架模型關(guān)聯(lián)的可用性的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Laravel等框架具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Thinkphp極驗(yàn)滑動(dòng)驗(yàn)證碼實(shí)現(xiàn)步驟解析
這篇文章主要介紹了Thinkphp極驗(yàn)滑動(dòng)驗(yàn)證碼實(shí)現(xiàn)步驟解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
bindParam和bindValue的區(qū)別以及在Yii2中的使用詳解
下面小編就為大家分享一篇bindParam和bindValue的區(qū)別以及在Yii2中的使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-03-03
PHP會(huì)員找回密碼功能的簡單實(shí)現(xiàn)
下面小編就為大家?guī)硪黄狿HP會(huì)員找回密碼功能的簡單實(shí)現(xiàn)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-09-09
php實(shí)現(xiàn)微信原生支付(掃碼支付)功能
這篇文章主要為大家詳細(xì)介紹了php實(shí)現(xiàn)微信原生支付,掃碼支付功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
PHP上傳Excel文件導(dǎo)入數(shù)據(jù)到MySQL數(shù)據(jù)庫示例
這篇文章主要介紹了PHP上傳Excel文件導(dǎo)入數(shù)據(jù)到MySQL數(shù)據(jù)庫示例,可以將Excel的數(shù)據(jù)寫入到MySQL數(shù)據(jù)庫中,感興趣的同學(xué)可以了解一下。2016-10-10
php實(shí)現(xiàn)的支付寶網(wǎng)頁支付功能示例【基于TP5框架】
這篇文章主要介紹了php實(shí)現(xiàn)的支付寶網(wǎng)頁支付功能,結(jié)合實(shí)例形式分析了基于TP5框架框架的支付寶網(wǎng)頁支付功能具體操作步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-09-09

