|
<?php
/**
* QQ Development Platform SDK
* Author: Occasionally intoxicated
* blog:www.stutostu.com
*/
Class qq_sdk{
Configure app Parameters
Private $app _id = your app id;
Private $app _secret = ' Your App_secret ';
Private $redirect = your callback address;
function __construct ()
{
}
/**
* [Get_access_token get Access_token]
* @param [string] $code [$_get[' code ' returned after landing]]
* @return [Array] [expires_in is a valid time, Access_token is the authorization code; failure returns error, Error_description]
*/
function Get_access_token ($code)
{
Get Access_token
$token _url = ' https://graph.qq.com/oauth2.0/token?grant_type=authorization_code& '
. ' Client_id= '. $this->app_id. ' &redirect_uri= '. UrlEncode ($this->redirect)/callback Address
. ' &client_secret= '. $this->app_secret. ' &code= '. $code;
$token = Array ();
Expires_in for Access_token active time increment
Parse_str ($this->_curl_get_content ($token _url), $token);
return $token;
}
/**
* [get_open_id get user only Id,openid]
* @param [string] $token [Authorization code]
* @return [Array] [successfully returned client_id and OpenID; failure returns error and ERROR_MSG]
*/
function get_open_id ($token)
{
$str = $this->_curl_get_content (' https://graph.qq.com/oauth2.0/me?access_token= '. $token);
if (Strpos ($str, "callback")!== false)
{
$lpos = Strpos ($str, "(");
$rpos = Strrpos ($str, ")");
$str = substr ($str, $lpos + 1, $rpos – $lpos-1);
}
$user = Json_decode ($str, TRUE);
return $user;
}
/**
* [Get_user_info get user Information]
* @param [string] $token [Authorization code]
* @param [string] $open _id [user unique ID]
* @return [array] [RET: Return code, successful at 0 o'clock. MSG is an error message and is empty when correctly returned. ... params]
*/
function Get_user_info ($token, $open _id)
{
Assembly URL
$user _info_url = ' Https://graph.qq.com/user/get_use
R_info? '
. ' Access_token= '. $token
. ' &oauth_consumer_key= '. $this->app_id
. ' &openid= '. $open _id
. ' &format=json ';
$info = Json_decode ($this->_curl_get_content ($user _info_url), TRUE);
return $info;
}
Private Function _curl_get_content ($url)
{
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_returntransfer, TRUE);
curl_setopt ($ch, Curlopt_url, $url);
Set timeout time to 3s
curl_setopt ($ch, Curlopt_connecttimeout, 3);
$result = curl_exec ($ch);
Curl_close ($ch);
return $result;
}
}
/* End of qq_sdk.php * *
|