Home > php教程 > php手册 > php QQ第三方登陆SDK程序代码

php QQ第三方登陆SDK程序代码

WBOY
Release: 2016-05-25 16:39:58
Original
1125 people have browsed it

自己重写的一个php QQ第三方登陆SDK程序代码,官方的不敢恭维了所以自己再写了一个,主要是考虑到QQ的PHP SDK写的真是太烂了,纯属是普及API知识,而不是到手就可以部署的类库,反正自己都写了一个了,就拿出来分享下.

什么也不多说,直接上代码,代码如下:

<?php
/** 
 * QQ开发平台 SDK
 * 作者:偶尔陶醉
 * blog: www.phprm.com
 */
class Qq_sdk {
    //配置APP参数
    private $app_id = 你的APPID;
    private $app_secret = &#39;你的APP_secret&#39;;
    private $redirect = 你的回调地址;
    function __construct() {
    }
    /** 
     * [get_access_token 获取access_token]
     * @param [string] $code [登陆后返回的$_GET[&#39;code&#39;]]
     * @return [array] [expires_in 为有效时间 , access_token 为授权码 ; 失败返回 error , error_description ]
     */
    function get_access_token($code) {
        //获取access_token
        $token_url = &#39;https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&&#39; . &#39;client_id=&#39; . $this->app_id . &#39;&redirect_uri=&#39; . urlencode($this->redirect) //回调地址
         . &#39;&client_secret=&#39; . $this->app_secret . &#39;&code=&#39; . $code;
        $token = array();
        //expires_in 为access_token 有效时间增量
        parse_str($this->_curl_get_content($token_url) , $token);
        return $token;
    }
    /** 
     * [get_open_id 获取用户唯一ID,openid]
     * @param [string] $token [授权码]
     * @return [array] [成功返回client_id 和 openid ;失败返回error 和 error_msg]
     */
    function get_open_id($token) {
        $str = $this->_curl_get_content(&#39;https://graph.qq.com/oauth2.0/me?access_token=&#39; . $token);
        if (strpos($str, "callback") !== false) {
            $lpos = strpos($str, "(");
            $rpos = strrpos($str, ")");
            $str = substr($str, $lpos + 1, $rpos & ndash;
            $lpos - 1);
        }
        $user = json_decode($str, TRUE);
        return $user;
    }
    /** 
     * [get_user_info 获取用户信息]
     * @param [string] $token [授权码]
     * @param [string] $open_id [用户唯一ID]
     * @return [array] [ret:返回码,为0时成功。msg为错误信息,正确返回时为空。...params]
     */
    function get_user_info($token, $open_id) {
        //组装URL
        $user_info_url = &#39;https://graph.qq.com/user/get_use 
r_info?&#39; . &#39;access_token=&#39; . $token . &#39;&oauth_consumer_key=&#39; . $this->app_id . &#39;&openid=&#39; . $open_id . &#39;&format=json&#39;;
        $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);
        //设置超时时间为3s
        //开源代码phprm.com
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }
}
/* end of Qq_sdk.php */
?>
Copy after login

使用方法:在你网站上放置超链接,地址为:https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=你的APP_ID&redirect_uri=你的回调地址.

在回调地址上调用我上面这个qq_sdk即可.

demo代码如下:

<?php
if (emptyempty($_GET[&#39;code&#39;])) {
    exit(&#39;参数非法&#39;);
}
include (&#39;qq_sdk&#39;);
$qq_sdk = new Qq_sdk();
$token = $qq_sdk->get_access_token($_GET[&#39;code&#39;]);
print_r($token);
$open_id = $qq_sdk->get_open_id($token[&#39;access_token&#39;]);
print_r($open_id);
$user_info = $qq_sdk->get_user_info($token[&#39;access_token&#39;], $open_id[&#39;openid&#39;]);
print_r($user_info);
?>
Copy after login


文章网址:

随意转载^^但请附上教程地址。

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template