PHP Oauth授權(quán)和本地加密實(shí)現(xiàn)方法
來源:易賢網(wǎng) 閱讀:927 次 日期:2016-08-29 14:32:05
溫馨提示:易賢網(wǎng)小編為您整理了“PHP Oauth授權(quán)和本地加密實(shí)現(xiàn)方法”,方便廣大網(wǎng)友查閱!

1.Oauth(開放授權(quán))是一個(gè)開放標(biāo)準(zhǔn),允許用戶讓第三方應(yīng)用訪問該用戶在某一網(wǎng)站上存儲(chǔ)的私密資源(如照片,視頻,聯(lián)系人列表),而無需將用戶名和密碼提供給第三方

關(guān)鍵字:appKey appSecret token(令牌)

2.SSO授權(quán)

如果本地手機(jī)裝有微博客戶端,則直接跳轉(zhuǎn)到微博客戶端,只需點(diǎn)擊授權(quán)按鈕,就可以登陸了 

qq第三方登陸使用Oauth2.0實(shí)現(xiàn),測試代碼

點(diǎn)擊下面的連接

https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=101334262&redirect_uri=http://www.qingguow.cn/sso.php

具體代碼sso.php文件:

<?php

// qq登陸類

class Sso{

  const APP_ID="101334262";

  const APP_KEY="xxxxxxxxxxxxxxx";

  //初始化

  public static function init(){

    header("content-type:text/html;charset=utf-8");

  }

    //主函數(shù)

  public static function main(){

    //請(qǐng)求控制

    $action=$_GET['action'];

    if(!empty($action)){

      Sso::$action();

      return;

    }

    $par = 'grant_type=authorization_code'

    . '&client_id='.Sso::APP_ID

    . '&client_secret='.Sso::APP_KEY

    . '&code='.$_REQUEST['code']

    . '&redirect_uri='.urlencode('http://www.qingguow.cn/sso.php');

    $rec=Sso::postUrlContents("https://graph.qq.com/oauth2.0/token",$par);

    if(strpos($rec, 'access_token') !== false) {

      parse_str($rec, $accessToken);

      $openidJson=Sso::getUrlContents("https://graph.qq.com/oauth2.0/me?callback=callback&access_token={$accessToken['access_token']}");

      $openidJson=str_replace("callback( ", "", $openidJson);

      $openidJson=str_replace(");", "", $openidJson);

      $openidJson=json_decode($openidJson,true);

      header("location:sso.php?action=getQQinfo&openid={$openidJson['openid']}&access_token={$accessToken['access_token']}");

    }

  }

  //獲取用戶信息

  public static function getQQinfo(){

    Sso::init();

    $openid=$_GET['openid'];

    $access_token=$_GET['access_token'];

    $userJson=Sso::getUrlContents("https://graph.qq.com/user/get_user_info?openid={$openid}&access_token={$access_token}&oauth_consumer_key=".Sso::APP_ID);

    $user=json_decode($userJson,true);

    print_r($user);

  }

  //get方式請(qǐng)求數(shù)據(jù)

  public static function getUrlContents($url){

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

    curl_setopt($ch, CURLOPT_HEADER, false);

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

    curl_setopt($ch, CURLOPT_URL, $url);

    curl_setopt($ch, CURLOPT_REFERER, $url);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

    $result = curl_exec($ch);

    curl_close($ch);

    return $result;

  }

  //post請(qǐng)求數(shù)據(jù)

  public static function postUrlContents($url,$data = null){

    $curl = curl_init();

    curl_setopt($curl, CURLOPT_URL, $url);

    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);

    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

    if (!empty($data)){

    curl_setopt($curl, CURLOPT_POST, 1);

    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

    }

    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    $output = curl_exec($curl);

    curl_close($curl);

    return $output;

  }

}

Sso::main();

以上這篇PHP Oauth授權(quán)和本地加密實(shí)現(xiàn)方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:PHP Oauth授權(quán)和本地加密實(shí)現(xiàn)方法
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65317125(9:00—18:00) 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)