欧美成人午夜免费全部完,亚洲午夜福利精品久久,а√最新版在线天堂,另类亚洲综合区图片小说区,亚洲欧美日韩精品色xxx

扣丁學堂PHP培訓簡述PHP調(diào)用微博接口實現(xiàn)微博登錄的方法

2019-09-09 10:17:48 5043瀏覽

在平時項目開發(fā)過程中,除了注冊本網(wǎng)站賬號進行登錄之外,還可以調(diào)用第三方接口進行登錄網(wǎng)站。文篇文章扣丁學堂PHP培訓小編以微博登錄為例為大家分享一下PHP調(diào)用微博接口實現(xiàn)微博登錄的方法。微博登錄包括身份認證、用戶關(guān)系以及內(nèi)容傳播。允許用戶使用微博帳號登錄訪問第三方網(wǎng)站,分享內(nèi)容,同步信息。


扣丁學堂PHP培訓簡述PHP調(diào)用微博接口實現(xiàn)微博登錄的方法


1、首先需要引導需要授權(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=***項目本地域名***/home/login/webLogin?";
    //授權(quán)后將頁面重定向到本地項目
    $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方式請求  替換參數(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" >點擊這里進行微博登錄</a>
</body>
</html>

想要了解更多關(guān)于PHP開發(fā)方面內(nèi)容的小伙伴,請關(guān)注扣丁學堂PHP培訓官網(wǎng)、微信等平臺,扣丁學堂IT職業(yè)在線學習教育有專業(yè)的PHP講師為您指導,此外扣丁學堂老師精心推出的PHP視頻教程定能讓你快速掌握PHP從入門到精通開發(fā)實戰(zhàn)技能??鄱W堂PHP技術(shù)交流群:374332265。


                          JavaEE/微服務(wù)/源碼解析/分布式/企業(yè)級架構(gòu)【VIP體驗課】


     【關(guān)注微信公眾號獲取更多學習資料】        【掃碼進入JavaEE/微服務(wù)VIP免費公開課】  



查看更多關(guān)于“php培訓資訊”的相關(guān)文章>>


標簽: PHP培訓 PHP視頻教程 PHP在線視頻 PHP學習視頻 Laravel框架

熱門專區(qū)

暫無熱門資訊

課程推薦

微信
微博
15311698296

全國免費咨詢熱線

郵箱:codingke@1000phone.com

官方群:148715490

北京千鋒互聯(lián)科技有限公司版權(quán)所有   北京市海淀區(qū)寶盛北里西區(qū)28號中關(guān)村智誠科創(chuàng)大廈4層
京ICP備2021002079號-2   Copyright ? 2017 - 2022
返回頂部 返回頂部