PHP version QQ interconnect OAuth sample code sharing _php Tips

Source: Internet
Author: User

Because of the universality of domestic QQ users, so now the major sites are as far as possible to provide QQ landing port, the following we look at the PHP version, for you to refer to the following

/** * QQ Interconnection OAuth * @author Dyllen * * * */class OAuth {//authorization Code Url const PC_CODE_URL = ' Https://graph
   
  . Qq.com/oauth2.0/authorize ';
   
  Take access Token Url const PC_ACCESS_TOKEN_URL = ' Https://graph.qq.com/oauth2.0/token ';
   
  Take the user Open Id Url const OPEN_ID_URL = ' https://graph.qq.com/oauth2.0/me ';
   
  The callback address public $redirectUri = null after user authorization;
   
  APP Id public $appid = null;
   
  APP Key public $appKey = null;
   
  Authorization list//string, multiple comma-separated public $scope = null;
   
  Authorization code public $code = NULL;
   
  Renew access token The voucher public $refreshToken = null;
   
  access token public $accessToken = null;
   
  Access token valid, unit seconds public $expiresIn = null;
   
  state public $state = null;
   
  public $openid = null;  Construct Public Function __construct ($config =[]) {foreach ($config as $key => $value) {$this-> $key
    = $value;
  }/** * Gets the URL to get code * @throws \invalidargumentexception * @return string/Public function Codeurl () {if (! $this->redirecturi) {throw new \exception (' Para
    Meter $redirectUri must be set. '); $query = [' Response_type ' => ' code ', ' client_id ' => $this->appid, ' redirect_uri ' =
   
    > $this->redirecturi, ' state ' => $this->getstate (), ' scope ' => $this->scope,]; Return self::P c_code_url. '?' .
  Http_build_query ($query);
    /** * Fetch 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::P c_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;
    /** * Refresh 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::P c_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;
 }
   
  /**  * User Open ID * @return String */Public Function Getopenid () {$params = [' Access_token ' => $t
   
    His->accesstoken,]; $url = Self::open_id_url. '?' .
       
    Http_build_query ($params);
     
    $this->openid = $this->parseopenid ($this->geturl ($url));
  return $this->openid;  /** * Get way URL content * @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 Way URL content * @param string $url * @param array $KEYSARR * @param number $flag * @return Mix
    Ed */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; /** * Takes state * @return string/protected function GetState () {$this->state = MD5 (uniqid
    (rand (), true);
   
    State has a cache inside//own definition//...
  return $this->state;
  /** * Verify State * @return Boolean/protected function verifystate () {//.....
    /** * Throws Exception * @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 ']); /** * resolves the OpenID * @param string $str * @return St from the return data obtained from the OpenID interfaceRing */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 '];
 }
}

The above is the entire contents of this article, I hope you can enjoy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.