-
申请地址 QQ互联
-
直接上代码
- 如下代码是放在application/libraries 若是你不是用的CI 框架也能够直接new
-
- QQ受权配置类 config.php
<?php header('Content-Type: text/html; charset=UTF-8'); /** * QQ 受权配置帐号信息 */ class Qqconfig { const QQ_APPID = '******'; //申请到的appid const QQ_KEY = '***********'; //申请到的appkey }
- QQ受权获取数据类
<?php header('Content-Type: text/html; charset=UTF-8'); /* * QQ受权登陆扩展 * */ if (!defined('BASEPATH')) exit('No direct script access allowed'); include 'qqconfig.php'; include 'qqrecorder.php'; class Qqouath { const GET_AUTH_CODE_URL = "https://graph.qq.com/oauth2.0/authorize"; const GET_ACCESS_TOKEN_URL = "https://graph.qq.com/oauth2.0/token"; const GET_OPENID_URL = "https://graph.qq.com/oauth2.0/me"; function __construct() { $this->recorder = new Qqrecorder(); $this->appid = Qqconfig::QQ_APPID; $this->key = Qqconfig::QQ_KEY; } public function qq_login($display = '', $g_ut = 1) { //-------构造请求参数列表 $keysArr = array( "response_type" => "code", "client_id" => $this->appid, "redirect_uri" => '', "state" => '', "scope" => 'get_user_info', 'display' => $display, 'g_ut' => $g_ut ); $login_url = $this->recorder->combineURL(self::GET_AUTH_CODE_URL, $keysArr); header("Location:$login_url"); } public function qq_callback($code = '', $return_state = '') { $state = $this->recorder->read("state"); //-------请求参数列表 $keysArr = array( "grant_type" => "authorization_code", "client_id" => $this->appid, "redirect_uri" => urlencode(''), "client_secret" => $this->key, "code" => $code ); //------构造请求access_token的url $token_url = $this->recorder->combineURL(self::GET_ACCESS_TOKEN_URL, $keysArr); $response = $this->recorder->get_contents($token_url); if (strpos($response, "callback") !== false) { $lpos = strpos($response, "("); $rpos = strrpos($response, ")"); $response = substr($response, $lpos + 1, $rpos - $lpos - 1); $msg = json_decode($response); if (isset($msg->error)) { die('获取数据错误' . $msg->error_description); } } $params = array(); parse_str($response, $params); return $params["access_token"]; } public function get_openid($access_token = '') { //-------请求参数列表 $keysArr = array( "access_token" => $access_token ); $graph_url = $this->recorder->combineURL(self::GET_OPENID_URL, $keysArr); $response = $this->recorder->get_contents($graph_url); //--------检测错误是否发生 if (strpos($response, "callback") !== false) { $lpos = strpos($response, "("); $rpos = strrpos($response, ")"); $response = substr($response, $lpos + 1, $rpos - $lpos - 1); } $user = json_decode($response); if (isset($user->error)) { die('数据openid错误:' . $user->error_description); } return $user->openid; } }
- QQ受权登陆 公共方法类
<?php /* PHP SDK * @version 2.0.0 * @author connect@qq.com * @copyright © 2013, Tencent Corporation. All rights reserved. */ class qqrecorder { private static $data; /** * combineURL * 拼接url * @param string $baseURL 基于的url * @param array $keysArr 参数列表数组 * @return string 返回拼接的url */ public function combineURL($baseURL, $keysArr) { $combined = $baseURL . "?"; $valueArr = array(); foreach ($keysArr as $key => $val) { $valueArr[] = "$key=$val"; } $keyStr = implode("&", $valueArr); $combined .= ($keyStr); return $combined; } /** * get_contents * 服务器经过get请求得到内容 * @param string $url 请求的url,拼接后的 * @return string 请求返回的内容 */ public function get_contents($url) { if (ini_get("allow_url_fopen") == "1") { $response = file_get_contents($url); } else { $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); } //-------请求为空 if (empty($response)) { $this->error->showError("50001"); } return $response; } /** * get * get方式请求资源 * @param string $url 基于的baseUrl * @param array $keysArr 参数列表数组 * @return string 返回的资源内容 */ public function get($url, $keysArr) { $combined = $this->combineURL($url, $keysArr); return $this->get_contents($combined); } /** * post * post方式请求资源 * @param string $url 基于的baseUrl * @param array $keysArr 请求的参数列表 * @param int $flag 标志位 * @return string 返回的资源内容 */ public function post($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; } }
-
后台调用方法php
class test extends CI_Controller { function __construct() { parent::__construct(); $this->load->library('qq_login_api/qqoauth'); } //QQ 登陆按钮跳转地址 public function Login_For_QQ_Oauth() { $this->qqoauth->qq_login(); } /* * QQ登陆回调地址 * 获取页面返回code * 经过code获取access_token * 经过access_token 获取openid * 判断是否存在该用户 */ public function Login_For_QQ() { if (!empty($_GET['code']) ) { $access_token = $this->qqoauth->qq_callback($_GET['code']); if (!empty($access_token)) { $openid = $this->qqoauth->get_openid($access_token); } } } }
- 前台调用
window.open("test/Login_For_QQ_Oauth", "TencentLogin", "width=450,height=320,menubar=0,scrollbars=1, resizable=1,status=1,titlebar=0,toolbar=0,location=1");
- QQ受权配置类 config.php