PHP版QQ登入口

來源:互聯網
上載者:User
本篇文章主要介紹PHP版QQ登入口,感興趣的朋友參考下,希望對大家有所協助。

由於國內QQ使用者的普遍性,所以現在各大網站都儘可能的提供QQ登陸口,下面我們來看看php版,給大家參考下

/** * QQ互聯 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';     //使用者授權之後的回調地址  public $redirectUri = null;     // App Id  public $appid = null;     //App Key  public $appKey = null;     //授權列表  //字串,多個用逗號隔開  public $scope = null;     //授權code  public $code = null;     //續期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內容   * @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內容   * @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介面的返回資料中解析出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'];  }}

總結:以上就是本篇文的全部內容,希望能對大家的學習有所協助。

相關推薦:

php實現針對html標籤中結束標籤的檢測與補全功能

php針對字串的轉換技巧

PHP實現針對指定尾碼檔案的批量上傳功能

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.