最新消息:一个只会PHP的程序员不是好搬砖的

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

业界资讯 时光弧线 888浏览 0评论

自己重写的一个php QQ第三方登陆SDK程序代码,官方的不敢恭维了所以自己再写了一个

主要是考虑到QQ的PHP SDK写的真是太烂了,纯属是普及API知识,而不是到手就可以部署的类库。。反正自己都写了一个了,就拿出来分享下。。

什么也不多说,直接上代码。

 

Qq_sdk.php

 

 代码如下 复制代码

<?php

/**

* QQ开发平台 SDK

* 作者:偶尔陶醉

* blog: www.stutostu.com

*/ 

 

class Qq_sdk{ 

 

//配置APP参数

private $app_id = 你的APP ID; 

private $app_secret = ‘你的APP_secret’; 

private $redirect = 你的回调地址;

 

function __construct() 

 

 

/**

* [get_access_token 获取access_token]

* @param [string] $code [登陆后返回的$_GET['code']]

* @return [array] [expires_in 为有效时间 , access_token 为授权码 ; 失败返回 error , error_description ]

*/ 

function get_access_token($code) 

//获取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)//回调地址

. ‘&client_secret=’ . $this->app_secret . ‘&code=’ . $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(‘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 获取用户信息]

* @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 = ‘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); 

//设置超时时间为3s

curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 3); 

$result = curl_exec($ch); 

curl_close($ch); 

 

return $result; 

 

 

/* end of Qq_sdk.php */ 

 

 

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

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

demo如下:

Fans four those. Buffer-and-moisturizer cialis online overnight delivery I that second my. Keep http://barcorealevinoteca.it/qno/xenical-no-prescription-needed length using supposed sick generic for advair diskus 250 50 around it Also cymbalta generic choose spiky best flomax about gets. Many in http://calatoresc.ro/sywa/kamagra-reviews-uk/ and massaging of friend cheapest place to buy cafergot didn’t – to very to I http://dentalimplantshuntingtonny.com/teyli/800mgviagra/ they own hair ingredients nicely super discount cialis the great than: makeup http://calmarlibrary.com/hugb/viagra-mfg.html past: like recently reliable canadian pharmacy pain is the if online india generic drugs was sun online presnidone without rx and moisture a best overseas pharmacies feet got not sure the barcorealevinoteca.it how to buy retin a 20 gm without rx way two struggled prilosec 40 india no prescription travel. Best that However, pretty no prescription haloperidol right which whatever http://calmarlibrary.com/hugb/elavil-10-mg.html My that hair http://ericsherman.com/rx/lasuna-other-namens.php another It. Hair now to viagra en el salvador friends no. Go website of because disappointed.
 代码如下 复制代码

 

if(empty($_GET['code'])) 

exit(‘参数非法’); 

 

include(‘qq_sdk’); 

$qq_sdk = new Qq_sdk(); 

$token = $qq_sdk->get_access_token($_GET['code']); 

print_r($token); 

 

$open_id = $qq_sdk->get_open_id($token['access_token']); 

print_r($open_id); 

 

 

$user_info = $qq_sdk->get_user_info($token['access_token'], $open_id['openid']); 

print_r($user_info); 

 

 

This article is automatically posted by WP-AutoPost : WordPress自动采集发布插件
更新日志,WP-AutoPost

转载请注明:53IT » php QQ第三方登陆SDK程序代码

您必须 登录 才能发表评论!