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

PHP版QQ互聯(lián)OAuth示例代碼分享

 更新時間:2015年07月05日 15:52:53   投稿:hebedich  
這篇文章主要介紹了PHP版QQ互聯(lián)OAuth示例代碼分享,十分的詳細使用,有需要的小伙伴可以參考下。

由于國內(nèi)QQ用戶的普遍性,所以現(xiàn)在各大網(wǎng)站都盡可能的提供QQ登陸口,下面我們來看看php版,給大家參考下

/**
 * QQ互聯(lián) oauth
 * @author dyllen
 *
 */
class Oauth
{
  //取Authorization Code Url
  const PC_CODE_URL = 'https://graph.qq.com/oauth2.0/authorize';
   
  //取Access Token Url
  const PC_ACCESS_TOKEN_URL = 'https://graph.qq.com/oauth2.0/token';
   
  //取用戶 Open Id Url
  const OPEN_ID_URL = 'https://graph.qq.com/oauth2.0/me';
   
  //用戶授權(quán)之后的回調(diào)地址
  public $redirectUri = null;
   
  // App Id
  public $appid = null;
   
  //App Key
  public $appKey = null;
   
  //授權(quán)列表
  //字符串,多個用逗號隔開
  public $scope = null;
   
  //授權(quán)code
  public $code = null;
   
  //續(xù)期access token的憑證
  public $refreshToken = null;
   
  //access token
  public $accessToken = null;
   
  //access token 有效期,單位秒
  public $expiresIn = null;
   
  //state
  public $state = null;
   
  public $openid = null;
   
  //construct
  public function __construct($config=[])
  {
    foreach($config as $key => $value) {
      $this->$key = $value;
    }
  }
   
  /**
   * 得到獲取Code的url
   * @throws \InvalidArgumentException
   * @return string
   */
  public function codeUrl()
  {
    if (!$this->redirectUri) {
      throw new \Exception('parameter $redirectUri must be set.');
    }
    $query = [
        'response_type' => 'code',
        'client_id' => $this->appid,
        'redirect_uri' => $this->redirectUri,
        'state' => $this->getState(),
        'scope' => $this->scope,
    ];
   
    return self::PC_CODE_URL . '?' . http_build_query($query);
  }
   
  /**
   * 取access token
   * @throws Exception
   * @return boolean
   */
  public function getAccessToken()
  {
    $params = [
        'grant_type' => 'authorization_code',
        'client_id' => $this->appid,
        'client_secret' => $this->appKey,
        'code' => $this->code,
        'redirect_uri' => $this->redirectUri,
    ];
   
    $url = self::PC_ACCESS_TOKEN_URL . '?' . http_build_query($params);
    $content = $this->getUrl($url);
    parse_str($content, $res);
    if ( !isset($res['access_token']) ) {
      $this->thrwoError($content);
    }
   
    $this->accessToken = $res['access_token'];
    $this->expiresIn = $res['expires_in'];
    $this->refreshToken = $res['refresh_token'];
   
    return true;
  }
   
  /**
   * 刷新access token
   * @throws Exception
   * @return boolean
   */
  public function refreshToken()
  {
    $params = [
        'grant_type' => 'refresh_token',
        'client_id' => $this->appid,
        'client_secret' => $this->appKey,
        'refresh_token' => $this->refreshToken,
    ];
   
    $url = self::PC_ACCESS_TOKEN_URL . '?' . http_build_query($params);
    $content = $this->getUrl($url);
    parse_str($content, $res);
    if ( !isset($res['access_token']) ) {
      $this->thrwoError($content);
    }
   
    $this->accessToken = $res['access_token'];
    $this->expiresIn = $res['expires_in'];
    $this->refreshToken = $res['refresh_token'];
   
    return true;
  }
   
  /**
   * 取用戶open id
   * @return string
   */
  public function getOpenid()
  {
    $params = [
        'access_token' => $this->accessToken,
    ];
   
    $url = self::OPEN_ID_URL . '?' . http_build_query($params);
       
    $this->openid = $this->parseOpenid( $this->getUrl($url) );
     
    return $this->openid;
  }
   
  /**
   * get方式取url內(nèi)容
   * @param string $url
   * @return mixed
   */
  public function getUrl($url)
  {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_URL, $url);
    $response = curl_exec($ch);
    curl_close($ch);
   
    return $response;
  }
   
  /**
   * post方式取url內(nèi)容
   * @param string $url
   * @param array $keysArr
   * @param number $flag
   * @return mixed
   */
  public function postUrl($url, $keysArr, $flag = 0)
  {
    $ch = curl_init();
    if(! $flag) curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $keysArr);
    curl_setopt($ch, CURLOPT_URL, $url);
    $ret = curl_exec($ch);
   
    curl_close($ch);
    return $ret;
  }
   
   
  /**
   * 取state
   * @return string
   */
  protected function getState()
  {
    $this->state = md5(uniqid(rand(), true));
    //state暫存在緩存里面
    //自己定義
        //。。。。。。。。。
   
    return $this->state;
  }
   
  /**
   * 驗證state
   * @return boolean
   */
  protected function verifyState()
  {
    //。。。。。。。
  }
   
  /**
   * 拋出異常
   * @param string $error
   * @throws \Exception
   */
  protected function thrwoError($error)
  {
    $subError = substr($error, strpos($error, "{"));
    $subError = strstr($subError, "}", true) . "}";
    $error = json_decode($subError, true);
     
    throw new \Exception($error['error_description'], (int)$error['error']);
  }
   
  /**
   * 從獲取openid接口的返回數(shù)據(jù)中解析出openid
   * @param string $str
   * @return string
   */
  protected function parseOpenid($str)
  {
    $subStr = substr($str, strpos($str, "{"));
    $subStr = strstr($subStr, "}", true) . "}";
    $strArr = json_decode($subStr, true);
    if(!isset($strArr['openid'])) {
      $this->thrwoError($str);
    }
     
    return $strArr['openid'];
  }
}

以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。

相關(guān)文章

  • php解決和避免form表單重復(fù)提交的幾種方法

    php解決和避免form表單重復(fù)提交的幾種方法

    在PHP提交表單的時候,可能遇到網(wǎng)速等導(dǎo)致頁面突然加載變慢,用戶重復(fù)地點擊提交按鈕,將在數(shù)據(jù)庫產(chǎn)生多條數(shù)據(jù),導(dǎo)致不可控情況。那么如何避免和解決這種問題呢?下面來一起看看。
    2016-08-08
  • 實例講解PHP面向?qū)ο笾鄳B(tài)

    實例講解PHP面向?qū)ο笾鄳B(tài)

    這篇文章主要介紹了實例講解PHP面向?qū)ο笾鄳B(tài),本文用實例講解什么情況下使用PHP的多態(tài)、多態(tài)的好處等內(nèi)容,可以充分幫你理解多態(tài),需要的朋友可以參考下
    2014-08-08
  • php array_chunk()函數(shù)用法與注意事項

    php array_chunk()函數(shù)用法與注意事項

    這篇文章主要介紹了php array_chunk()函數(shù)用法與注意事項,結(jié)合實例形式分析了php數(shù)組分割函數(shù)array_chunk()相關(guān)功能、用法及操作注意事項,需要的朋友可以參考下
    2019-07-07
  • php中生成隨機密碼的自定義函數(shù)代碼

    php中生成隨機密碼的自定義函數(shù)代碼

    這篇文章主要分享下php中生成隨機密碼的方法,原理就是把一些要生成的字符預(yù)置一個的字符串包括數(shù)字拼音之類的以及一些特殊字符,這樣我們再隨機取字符組成我們想要的隨機密碼了
    2013-10-10
  • PHP中使用file_get_contents抓取網(wǎng)頁中文亂碼問題解決方法

    PHP中使用file_get_contents抓取網(wǎng)頁中文亂碼問題解決方法

    這篇文章主要介紹了PHP中使用file_get_contents抓取網(wǎng)頁中文亂碼問題解決方法,可以通過使用curl配置gzip選項來解決,具有一定的參考借鑒價值,需要的朋友可以參考下
    2014-12-12
  • PHP解決中文亂碼

    PHP解決中文亂碼

    在php中,中文亂碼非常頭疼,很麻煩,所以根據(jù)在編程的經(jīng)驗,總結(jié)以下方法(以utf_8為例),下面跟著小編一起來看下吧
    2017-04-04
  • php中文字符串截取多種方法匯總

    php中文字符串截取多種方法匯總

    這篇文章主要為大家詳細介紹了php中文字符串截取多種方法,具有一定的參考價值,感興趣的朋友可以參考一下
    2016-10-10
  • PHP獲取Exif縮略圖的方法

    PHP獲取Exif縮略圖的方法

    這篇文章主要介紹了PHP獲取Exif縮略圖的方法,實例分析了php針對圖片的讀取及返回MIME類型的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-07-07
  • PHP基于curl后臺遠程登錄正方教務(wù)系統(tǒng)的方法

    PHP基于curl后臺遠程登錄正方教務(wù)系統(tǒng)的方法

    這篇文章主要介紹了PHP基于curl后臺遠程登錄正方教務(wù)系統(tǒng)的方法,結(jié)合實例形式分析了php使用curl及cookie實現(xiàn)遠程登陸的操作技巧,需要的朋友可以參考下
    2016-10-10
  • RSA實現(xiàn)JS前端加密與PHP后端解密功能示例

    RSA實現(xiàn)JS前端加密與PHP后端解密功能示例

    這篇文章主要介紹了RSA實現(xiàn)JS前端加密與PHP后端解密功能,結(jié)合實例形式分析了rsa前端js加密與后端php解密相關(guān)操作技巧,需要的朋友可以參考下
    2019-08-08

最新評論