主頁 > 知識庫 > PHP調用微博接口實現微博登錄的方法示例

PHP調用微博接口實現微博登錄的方法示例

熱門標簽:神龍斗士電話機器人 宿州正規(guī)外呼系統(tǒng)軟件 電信外呼系統(tǒng)多少錢一個月 代理打電話機器人 企業(yè)400電話辦理多少費用 太原400電話申請流程 合肥企業(yè)外呼系統(tǒng)線路 萍鄉(xiāng)商鋪地圖標注 桂陽公司如何做地圖標注

在平時項目開發(fā)過程中,除了注冊本網站賬號進行登錄之外,還可以調用第三方接口進行登錄網站。這里以微博登錄為例。微博登錄包括身份認證、用戶關系以及內容傳播。允許用戶使用微博帳號登錄訪問第三方網站,分享內容,同步信息。

1、首先需要引導需要授權的用戶到如下地址:

https://api.weibo.com/oauth2/authorize?client_id=YOUR_CLIENT_IDresponse_type=coderedirect_uri=YOUR_REGISTERED_REDIRECT_URI

如果用戶同意授權,頁面跳轉至 YOUR_REGISTERED_REDIRECT_URI/?code=CODE:

2、接下來要根據上面得到的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調用API,獲取用戶身份,完成用戶的登錄。

話不多說,直接上代碼:

為了方便,我們先將get和post封裝到application下的common.php中:
應用公共文件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 = "***微博應用安全域名***/?backurl=***項目本地域名***/home/login/webLogin?";
    //授權后將頁面重定向到本地項目
    $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方式請求  替換參數: 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方法,替換參數: 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>

效果圖:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • PHP實現發(fā)送微博消息功能完整示例
  • redis+php實現微博(三)微博列表功能詳解
  • redis+php實現微博(二)發(fā)布與關注功能詳解
  • redis+php實現微博(一)注冊與登錄功能詳解
  • PHP+redis實現微博的拉模型案例詳解
  • PHP+redis實現微博的推模型案例分析
  • vue+php實現的微博留言功能示例
  • php微信分享到朋友圈、QQ、朋友、微博
  • php新浪微博登錄接口用法實例
  • 基于PHP實現發(fā)微博動態(tài)代碼實例

標簽:崇左 廊坊 衡陽 白銀 鄂州 綏化 辛集 太原

巨人網絡通訊聲明:本文標題《PHP調用微博接口實現微博登錄的方法示例》,本文關鍵詞  PHP,調用,微博,接口,實現,;如發(fā)現本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《PHP調用微博接口實現微博登錄的方法示例》相關的同類信息!
  • 本頁收集關于PHP調用微博接口實現微博登錄的方法示例的相關信息資訊供網民參考!
  • 推薦文章