<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 黑猫 <416148489@qq.com>
// +----------------------------------------------------------------------
/**
* PHP SDK for weibo.com (using OAuth2)
*
* @author Elmer Zhang <freeboy6716@gmail.com>
*/
/**
* If the class OAuthException has not been declared, extend the Exception class.
* This is to allow OAuth without the PECL OAuth library
*
* @ignore
*/
if ( ! class_exists( 'OAuthException')) {
class OAuthException extends Exception {
// pass
}
}
/**
* 新浪微博 OAuth 认证类(OAuth2)
*
* 授权机制说明请大家参考微博开放平台文档:{@link http://open.weibo.com/wiki/Oauth2}
*
* @package sae
* @author Elmer Zhang
* @version 1.0
*/
class SaeTOAuthV2 {
/**
* @ignore
*/
public $client_id;
/**
* @ignore
*/
public $client_secret;
/**
* @ignore
*/
public $access_token;
/**
* @ignore
*/
public $refresh_token;
/**
* Contains the last HTTP status code returned.
*
* @ignore
*/
public $http_code;
/**
* Contains the last API call.
*
* @ignore
*/
public $url;
/**
* Set up the API root URL.
*
* @ignore
*/
public $host = "https://api.weibo.com/2/";
/**
* Set timeout default.
*
* @ignore
*/
public $timeout = 30;
/**
* Set connect timeout.
*
* @ignore
*/
public $connecttimeout = 30;
/**
* Verify SSL Cert.
*
* @ignore
*/
public $ssl_verifypeer = FALSE;
/**
* Respons format.
*
* @ignore
*/
public $format = 'json';
/**
* Decode returned json data.
*
* @ignore
*/
public $decode_json = TRUE;
/**
* Contains the last HTTP headers returned.
*
* @ignore
*/
public $http_info;
/**
* Set the useragnet.
*
* @ignore
*/
public $useragent = 'Sae T OAuth2 v0.1';
/**
* print the debug info
*
* @ignore
*/
public $debug = FALSE;
/**
* boundary of multipart
* @ignore
*/
public static $boundary = '';
/**
* Set API URLS
*/
/**
* @ignore
*/
function accessTokenURL() { return 'https://api.weibo.com/oauth2/access_token'; }
/**
* @ignore
*/
function authorizeURL() { return 'https://api.weibo.com/oauth2/authorize'; }
/**
* construct WeiboOAuth object
*/
function __construct($client_id, $client_secret, $access_token = NULL, $refresh_token = NULL) {
$this->client_id = $client_id;
$this->client_secret = $client_secret;
$this->access_token = $access_token;
$this->refresh_token = $refresh_token;
}
/**
* authorize接口
*
* 对应API:{@link http://open.weibo.com/wiki/Oauth2/authorize Oauth2/authorize}
*
* @param string $url 授权后的回调地址,站外应用需与回调地址一致,站内应用需要填写canvas page的地址
* @param string $response_type 支持的值包括 code 和token 默认值为code
* @param string $state 用于保持请求和回调的状态。在回调时,会在Query Parameter中回传该参数
* @param string $display 授权页面类型 可选范围:
* - default 默认授权页面
* - mobile 支持html5的手机
* - popup 弹窗授权页
* - wap1.2 wap1.2页面
* - wap2.0 wap2.0页面
* - js js-sdk 专用 授权页面是弹窗,返回结果为js-sdk回掉函数
* - apponweibo 站内应用专用,站内应用不传display参数,并且response_type为token时,默认使用改display.授权后不会返回access_token,只是输出js刷新站内应用父框架
* @return array
*/
function getAuthorizeURL( $url, $response_type = 'code', $state = NULL, $display = NULL ) {
$params = array();
$params['client_id'] = $this->client_id;
$params['redirect_uri'] = $url;
$params['response_type'] = $response_type;
$params['state'] = $state;
$params['display'] = $display;
return $this->authorizeURL() . "?" . http_build_query($params);
}
/**
* access_token接口
*
* 对应API:{@link http://open.weibo.com/wiki/OAuth2/access_token OAuth2/access_token}
*
* @param string $type 请求的类型,可以为:code, password, token
* @param array $keys 其他参数:
* - 当$type为code时: array('code'=>..., 'redirect_uri'=>...)
* - 当$type为password时: array('username'=>..., 'password'=>...)
* - 当$type为token时: array('refresh_token'=>...)
* @return array
*/
function getAccessToken( $type = 'code', $keys ) {
$params = array();
$params['client_id'] = $this->client_id;
$params['client_secret'] = $this->client_secret;
if ( $type === 'token' ) {
$params['grant_type'] = 'refresh_token';
$params['refresh_token'] = $keys['refresh_token'];
} elseif ( $type === 'code' ) {
$params['grant_type'] = 'authorization_code';
$params['code'] = $keys['code'];
$params['redirect_uri'] = $keys['redirect_uri'];
} elseif ( $type === 'password' ) {
$params['grant_type'] = 'password';
$params['username'] = $keys['username'];
$params['password'] = $keys['password'];
} else {
throw new OAuthException("wrong auth type");
}
$response = $this->oAuthRequest($this->accessTokenURL(), 'POST', $params);
$token = json_decode($response, true);
if ( is_array($token) && !isset($token['error']) ) {
$this->access_token = $token['access_token'];
//$this->refresh_token = $token['refresh_token'];
} else {
throw new OAuthException("get access token failed." . $token['error']);
}
return $token;
}
/**
* 解析 signed_request
*
* @param string $signed_request 应用框架在加载iframe时会通过向Canvas URL post的参数signed_request
*
* @return array
*/
function parseSignedRequest($signed_request) {
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$sig = self::base64decode($encoded_sig) ;
$data = json_decode(self::base64decode($payload), true);
if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') return '-1';
$expected_sig = hash_hmac('sha256', $payload, $this->client_secret, true);
return ($sig !== $expected_sig)? '-2':$data;
}
/**
* @ignore
*/
function base64decode($str) {
return base64_decode(strtr($str.str_repeat('=', (4 - strlen($str) % 4)), '-_', '+/'));
}
/**
* 读取jssdk授权信息,用于和jssdk的同步登录
*
* @return array 成功返回array('access_token'=>'value', 'refresh_token'=>'value'); 失败返回false
*/
function getTokenFromJSSDK() {
$key = "weibojs_" . $this->client_id;
if ( isset($_COOKIE[$key]) && $cookie = $_COOKIE[$key] ) {
parse_str($cookie, $token);
if ( isset($token['access_token']) && isset($token['refresh_token']) ) {
$this->access_token = $token['access_token'];
$this->refresh_token = $token['refresh_token'];
return $token;
} else {
return false;
}
} else {
return false;
}
}
/**
* 从数组中读取access_token和refresh_token
* 常用于从Session或Cookie中读取token,或通过Session/Cookie中是否存有token判断登录状态。
*
* @param array $arr 存有access_token和secret_token的数组
* @return array 成功返回array('access_token'=>'value', 'refresh_token'=>'value'); 失败返回false
*/
function getTokenFromArray( $arr ) {
if (isset($arr['access_token']) && $arr['access_token']) {
$token = array();
$this->access_token = $token['access_token'] = $arr['access_token'];
if (isset($arr['refresh_token']) && $arr['refresh_token']) {
$this->refresh_token = $token['refresh_token'] = $arr['refresh_token'];
}
return $token;
} else {
return false;
}
}
/**
* GET wrappwer for oAuthRequest.
*
* @return mixed
*/
function get($url, $parameters = array())
破碎的天堂鸟
- 粉丝: 1w+
- 资源: 3024
最新资源
- 玻璃盘CCD影像筛选机程序:5套视觉系统,稳定可靠,千台装机经验,视觉定位检测经典实机程序,玻璃盘CCD影像筛选机程序,应用5套CCD视觉系统,上位机工控电脑采用IO板转通讯输出OK NG信号,此设备
- 卡盖连接器产品设计规范
- 三菱FX Q FX5U PLC程序加密保护系统:ST结构化文本编程,实现授权码验证、时间管理、触摸屏提醒与通信监控、程序块软加密及跨PLC平台移植,三菱FX Q FX5U PLC 程序加密,使用ST结
- 弹片产品设计规范资源学习
- "远程PLC监控调试与多客户端TCP中转服务器的实现:基于通用中转服务器与Socket多线程并发通讯技术",远程PLC监控调试,PLC通用中转服务器,多客户端tcp中转服务器源代码,socket多线程
- 步进伺服电机控制程序:西门子PLC与威伦触摸屏驱动的步进电机正反转、定位与复位控制程序设置,步进伺服电机控制程序: 1.步进电机的正转、反转控制 2.相对和绝对位置运动 3.电机复位找回零点功能 4
- SIM卡连接器产品设计规范及制造工艺详解
- 三相整流器MATLAB仿真模型预测研究:电路设计与性能分析,三相整流器MATLAB仿真 图中为基于模型预测的三相整流器仿真模型 ,核心关键词:三相整流器; MATLAB仿真; 模型预测; 仿真模型
- libquadmath-devel-4.8.5-44.el7.x64-86.rpm.tar.gz
- libquadmath-static-4.8.5-44.el7.x64-86.rpm.tar.gz
- 组态王自动化数据记录与报表展示:条件触发存储至Excel,精确到秒的时间戳命名文件,报表控件实时更新展示,组态王条件触发数据记录,记录数据后,条件触发存储到excel表格,存储文件名为出发时的年月日时
- 基于Kingscada的外部数据库链接与报表系统实践:数据库处理、控件运用及与上位机链接的脚本实现,kingscada链接外部数据库处理以及报表系统,案例涉及到数据记录插入数据库的脚本实现方法,数据查
- MATLAB下蜻蜓算法优化广义回归神经网络的预测模型:DA-GRN N回归新探,MATLAB程序,蜻蜓算法优化广义回归神经网络,DA-GRN N,回归预测 ,关键词:MATLAB程序;蜻蜓算法;广义
- 艾默生充电桩15kw模块与台达三相PFC源码全套资料:核心算法、原理图、PCB及软件源码完全配套解析,艾默生充电桩15kw模块+台达三相PFC源程序 艾默生充电桩 15kw模块+台达三相PFC源码,软
- 基于MATLAB+CPLEX gurobi平台的SOE算法多时段随机配电网重构优化方法:降低网损,提高经济效益,MATLAB代码:基于SOE算法的多时段随机配电网重构方法 关键词:配电网重构 SOE算
- Modbus TCP协议转RTU串口通讯Arduino源码实现:一键智能配网WiFi连接与永久记忆功能,Modbus TCP协议转RTU串口通讯 TCP转RTU 程序里包含了常用命令的处理,源码采用
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈