搜索
系统检测到您的用户名不符合规范:

onethink中thinkphp 3.2版本,thinksdk报错

浏览:2594 发布日期:2014年08月31日 分类:求助交流

IndexController.class.php<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;
use OT\DataDictionary;
use ORG\ThinkSDK\ThinkOauth;
/**
* 前台首页控制器
* 主要获取首页聚合数据
*/
class IndexController extends HomeController {

//系统首页
public function index(){

$category = D('Category')->getTree();
$lists = D('Document')->lists(null);

$this->assign('category',$category);//栏目
$this->assign('lists',$lists);//列表
$this->assign('page',D('Document')->page);//分页


$this->display('index');
}

//登录地址
public function login($type = null){
empty($type) && $this->error('参数错误');

//加载ThinkOauth类并实例化一个对象
import("ORG.ThinkSDK.ThinkOauth");
$sns = ThinkOauth::getInstance($type);

//跳转到授权页面
redirect($sns->getRequestCodeURL());
}

//授权回调地址
public function callback($type = null, $code = null){
(empty($type) || empty($code)) && $this->error('参数错误');

//加载ThinkOauth类并实例化一个对象
import("ORG.ThinkSDK.ThinkOauth");
$sns = ThinkOauth::getInstance($type);

//腾讯微博需传递的额外参数
$extend = null;
if($type == 'tencent'){
$extend = array('openid' => $this->_get('openid'), 'openkey' => $this->_get('openkey'));
}

//请妥善保管这里获取到的Token信息,方便以后API调用
//调用方法,实例化SDK对象的时候直接作为构造函数的第二个参数传入
//如: $qq = ThinkOauth::getInstance('qq', $token);
$token = $sns->getAccessToken($code , $extend);

//获取当前登录用户信息
if(is_array($token)){
$user_info = A('Type', 'Event')->$type($token);

echo("<h1>恭喜!使用 {$type} 用户登录成功</h1><br>");
echo("授权信息为:<br>");
dump($token);
echo("当前登录用户信息为:<br>");
dump($user_info);
}
}


}
ThinkOauth.class.php<?php
// +----------------------------------------------------------------------
// | TOPThink [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2010 http://topthink.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi.cn@gmail.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------
// | ThinkOauth.class.php 2013年02月25日
// +----------------------------------------------------------------------
namespace ORG\ThinkSDK;

abstract class ThinkOauth{
/**
* oauth版本
* @var string
*/
protected $Version = '2.0';

/**
* 申请应用时分配的app_key
* @var string
*/
protected $AppKey = '';

/**
* 申请应用时分配的 app_secret
* @var string
*/
protected $AppSecret = '';

/**
* 授权类型 response_type 目前只能为code
* @var string
*/
protected $ResponseType = 'code';

/**
* grant_type 目前只能为 authorization_code
* @var string
*/
protected $GrantType = 'authorization_code';

/**
* 回调页面URL 可以通过配置文件配置
* @var string
*/
protected $Callback = '';

/**
* 获取request_code的额外参数 URL查询字符串格式
* @var srting
*/
protected $Authorize = '';

/**
* 获取request_code请求的URL
* @var string
*/
protected $GetRequestCodeURL = '';

/**
* 获取access_token请求的URL
* @var string
*/
protected $GetAccessTokenURL = '';

/**
* API根路径
* @var string
*/
protected $ApiBase = '';

/**
* 授权后获取到的TOKEN信息
* @var array
*/
protected $Token = null;

/**
* 调用接口类型
* @var string
*/
private $Type = '';

/**
* 构造方法,配置应用信息
* @param array $token
*/
public function __construct($token = null){
//设置SDK类型
$class = get_class($this);
$this->Type = strtoupper(substr($class, 0, strlen($class)-3));

//获取应用配置
$config = C("THINK_SDK_{$this->Type}");
if(empty($config['APP_KEY']) || empty($config['APP_SECRET'])){
throw new Exception('请配置您申请的APP_KEY和APP_SECRET');
} else {
$this->AppKey = $config['APP_KEY'];
$this->AppSecret = $config['APP_SECRET'];
$this->Token = $token; //设置获取到的TOKEN
}
}

/**
* 取得Oauth实例
* @static
* @return mixed 返回Oauth
*/
public static function getInstance($type, $token = null) {
$name = ucfirst(strtolower($type)) . 'SDK';
require_once "sdk/{$name}.class.php";
if (class_exists($name)) {
return new $name($token);
} else {
E(L('_CLASS_NOT_EXIST_') . ':' . $name);
}
}

/**
* 初始化配置
*/
private function config(){
$config = C("THINK_SDK_{$this->Type}");
if(!empty($config['AUTHORIZE']))
$this->Authorize = $config['AUTHORIZE'];
if(!empty($config['CALLBACK']))
$this->Callback = $config['CALLBACK'];
else
throw new Exception('请配置回调页面地址');
}

/**
* 请求code
*/
public function getRequestCodeURL(){
$this->config();
//Oauth 标准参数
$params = array(
'client_id' => $this->AppKey,
'redirect_uri' => $this->Callback,
'response_type' => $this->ResponseType,
);

//获取额外参数
if($this->Authorize){
parse_str($this->Authorize, $_param);
if(is_array($_param)){
$params = array_merge($params, $_param);
} else {
throw new Exception('AUTHORIZE配置不正确!');
}
}
return $this->GetRequestCodeURL . '?' . http_build_query($params);
}

/**
* 获取access_token
* @param string $code 上一步请求到的code
*/
public function getAccessToken($code, $extend = null){
$this->config();
$params = array(
'client_id' => $this->AppKey,
'client_secret' => $this->AppSecret,
'grant_type' => $this->GrantType,
'code' => $code,
'redirect_uri' => $this->Callback,
);

$data = $this->http($this->GetAccessTokenURL, $params, 'POST');
$this->Token = $this->parseToken($data, $extend);
return $this->Token;
}

/**
* 合并默认参数和额外参数
* @param array $params 默认参数
* @param array/string $param 额外参数
* @return array:
*/
protected function param($params, $param){
if(is_string($param))
parse_str($param, $param);
return array_merge($params, $param);
}

/**
* 获取指定API请求的URL
* @param string $api API名称
* @param string $fix api后缀
* @return string 请求的完整URL
*/
protected function url($api, $fix = ''){
return $this->ApiBase . $api . $fix;
}

/**
* 发送HTTP请求方法,目前只支持CURL发送请求
* @param string $url 请求URL
* @param array $params 请求参数
* @param string $method 请求方法GET/POST
* @return array $data 响应数据
*/
protected function http($url, $params, $method = 'GET', $header = array(), $multi = false){
$opts = array(
CURLOPT_TIMEOUT => 30,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_HTTPHEADER => $header
);

/* 根据请求类型设置特定参数 */
switch(strtoupper($method)){
case 'GET':
$opts[CURLOPT_URL] = $url . '?' . http_build_query($params);
break;
case 'POST':
//判断是否传输文件
$params = $multi ? $params : http_build_query($params);
$opts[CURLOPT_URL] = $url;
$opts[CURLOPT_POST] = 1;
$opts[CURLOPT_POSTFIELDS] = $params;
break;
default:
throw new Exception('不支持的请求方式!');
}

/* 初始化并执行curl请求 */
$ch = curl_init();
curl_setopt_array($ch, $opts);
$data = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
if($error) throw new Exception('请求发生错误:' . $error);
return $data;
}

/**
* 抽象方法,在SNSSDK中实现
* 组装接口调用参数 并调用接口
*/
abstract protected function call($api, $param = '', $method = 'GET', $multi = false);

/**
* 抽象方法,在SNSSDK中实现
* 解析access_token方法请求后的返回值
*/
abstract protected function parseToken($result, $extend);

/**
* 抽象方法,在SNSSDK中实现
* 获取当前授权用户的SNS标识
*/
abstract public function openid();
}
GoogleSDK.class.php<?php
// +----------------------------------------------------------------------
// | TOPThink [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2010 http://topthink.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi.cn@gmail.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------
// | GoogleSDK.class.php 2013年02月26日
// +----------------------------------------------------------------------
namespace ORG\ThinkSDK\sdk;
use ORG\ThinkSDK\ThinkOauth;
class GoogleSDK extends ThinkOauth{
/**
* 获取requestCode的api接口
* @var string
*/
protected $GetRequestCodeURL = 'https://accounts.google.com/o/oauth2/auth';

/**
* 获取access_token的api接口
* @var string
*/
protected $GetAccessTokenURL = 'https://accounts.google.com/o/oauth2/token';

/**
* 获取request_code的额外参数 URL查询字符串格式
* @var srting
*/
protected $Authorize = 'scope=https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email';

/**
* API根路径
* @var string
*/
protected $ApiBase = 'https://www.googleapis.com/oauth2/v1/';

/**
* 组装接口调用参数 并调用接口
* @param string $api 微博API
* @param string $param 调用API的额外参数
* @param string $method HTTP请求方法 默认为GET
* @return json
*/
public function call($api, $param = '', $method = 'GET', $multi = false){
/* Google 调用公共参数 */
$params = array();
$header = array("Authorization: Bearer {$this->Token['access_token']}");

$data = $this->http($this->url($api), $this->param($params, $param), $method, $header);
return json_decode($data, true);
}

/**
* 解析access_token方法请求后的返回值
* @param string $result 获取access_token的方法的返回值
*/
protected function parseToken($result, $extend){
$data = json_decode($result, true);
if($data['access_token'] && $data['token_type'] && $data['expires_in']){
$this->Token = $data;
$data['openid'] = $this->openid();
return $data;
} else
throw new Exception("获取 Google ACCESS_TOKEN出错:未知错误");
}

/**
* 获取当前授权应用的openid
* @return string
*/
public function openid(){
if(isset($this->Token['openid']))
return $this->Token['openid'];

$data = $this->call('userinfo');
if(!empty($data['id']))
return $data['id'];
else
throw new Exception('没有获取到 Google 用户ID!');
}

}
这是啥问题?纠结好半天了。
最佳答案
评论() 相关
后面还有条评论,
评论支持使用[code][/code]标签添加代码
您需要登录后才可以评论 登录 | 立即注册
收藏
mixadesign
积分:120 等级:LV1
热点推荐
(追記) (追記ここまで)
最新更新

我们

合作

网站

信息

ThinkPHP 是一个免费开源的,快速、简单的面向对象的 轻量级PHP开发框架 ,创立于2006年初,遵循Apache2开源协议发布,是为了敏捷WEB应用开发和简化企业应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。并且拥有众多的原创功能和特性,在社区团队的积极参与下,在易用性、扩展性和性能方面不断优化和改进,已经成长为国内最领先和最具影响力的WEB应用开发框架,众多的典型案例确保可以稳定用于商业以及门户级的开发。

AltStyle によって変換されたページ (->オリジナル) /