欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP調(diào)用微博接口實現(xiàn)微博登錄的方法示例

 更新時間:2018年09月22日 11:26:11   作者:JONGTY  
這篇文章主要介紹了PHP調(diào)用微博接口實現(xiàn)微博登錄的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

在平時項目開發(fā)過程中,除了注冊本網(wǎng)站賬號進(jìn)行登錄之外,還可以調(diào)用第三方接口進(jìn)行登錄網(wǎng)站。這里以微博登錄為例。微博登錄包括身份認(rèn)證、用戶關(guān)系以及內(nèi)容傳播。允許用戶使用微博帳號登錄訪問第三方網(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=***項目本地域名***/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" >點擊這里進(jìn)行微博登錄</a>
</body>
</html>

效果圖:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

  • PHP開發(fā)框架kohana3 自定義路由設(shè)置示例

    PHP開發(fā)框架kohana3 自定義路由設(shè)置示例

    這篇文章主要介紹了PHP開發(fā)框架kohana3 自定義路由設(shè)置示例,kohana是一個純PHP5的開發(fā)框架,需要的朋友可以參考下
    2014-07-07
  • PHP 實現(xiàn)類似js中alert() 提示框

    PHP 實現(xiàn)類似js中alert() 提示框

    這篇文章主要介紹了PHP 實現(xiàn)類似js中alert() 提示框功能,非常的實用,這里推薦給大家,有需要的小伙伴來參考下,希望大家能喜歡。
    2015-03-03
  • 用PHP寫的一個冒泡排序法的函數(shù)簡單實例

    用PHP寫的一個冒泡排序法的函數(shù)簡單實例

    下面小編就為大家?guī)硪黄肞HP寫的一個冒泡排序法的函數(shù)簡單實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-05-05
  • Laravel下生成驗證碼的類

    Laravel下生成驗證碼的類

    這篇文章主要為大家詳細(xì)介紹了Laravel下生成驗證碼的類,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • yii2.0實現(xiàn)創(chuàng)建簡單widgets示例

    yii2.0實現(xiàn)創(chuàng)建簡單widgets示例

    這篇文章主要介紹了yii2.0實現(xiàn)創(chuàng)建簡單widgets的方法,結(jié)合實例形式分析了Yii中widgets的基本創(chuàng)建及使用方法,需要的朋友可以參考下
    2016-07-07
  • 基于PHP實現(xiàn)通過照片獲取ip地址

    基于PHP實現(xiàn)通過照片獲取ip地址

    在本教程中,我們將學(xué)習(xí)如何用一張照片來盜取ip地址。我的想法是通過修改.htaccess文件,將jpg文件當(dāng)作php文件來解析。感興趣的朋友一起學(xué)習(xí)吧
    2016-04-04
  • PHP設(shè)計模式(四)原型模式Prototype實例詳解【創(chuàng)建型】

    PHP設(shè)計模式(四)原型模式Prototype實例詳解【創(chuàng)建型】

    這篇文章主要介紹了PHP設(shè)計模式:原型模式Prototype,結(jié)合實例形式詳細(xì)分析了PHP原型模式Prototype的基本概念、功能、原理、實現(xiàn)方法與操作注意事項,需要的朋友可以參考下
    2020-05-05
  • php基于CodeIgniter實現(xiàn)圖片上傳、剪切功能

    php基于CodeIgniter實現(xiàn)圖片上傳、剪切功能

    這篇文章主要為大家詳細(xì)介紹了php基于CodeIgniter實現(xiàn)圖片上傳、剪切功能,具有參考價值,感興趣的朋友可以參考一下
    2016-05-05
  • Laravel執(zhí)行migrate命令提示:No such file or directory的解決方法

    Laravel執(zhí)行migrate命令提示:No such file or directory的解決方法

    這篇文章主要介紹了Laravel執(zhí)行migrate命令提示:No such file or directory的解決方法,分析了執(zhí)行migrate命令出現(xiàn)錯誤的原因與相關(guān)的解決方法,需要的朋友可以參考下
    2016-03-03
  • 最新評論