PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/system/Services/Tencent/OpenSDK/Tencent/Weibo.php

https://github.com/sony88/answion
PHP | 261 lines | 133 code | 13 blank | 115 comment | 20 complexity | 399acfcf5579cce7a1e82aa51320d043 MD5 | raw file
  1. <?php
  2. /**
  3. * Tencent 微博 SDK
  4. *
  5. * 依赖:
  6. * 1、PECL json >= 1.2.0 no need now
  7. * 2、PHP >= 5.2.0 because json_decode no need now
  8. * 3、$_SESSION
  9. * 4、PECL hash >= 1.1 no need now
  10. *
  11. * only need PHP >= 5.0
  12. *
  13. * 如何使用:
  14. * 1、将OpenSDK文件夹放入include_path
  15. * 2、require_once 'OpenSDK/Tencent/Weibo.php';
  16. * 3、Services_Tencent_OpenSDK_Tencent_Weibo::init($appkey,$appsecret);
  17. * 4、Services_Tencent_OpenSDK_Tencent_Weibo::getRequestToken($callback); 获得request token
  18. * 5、Services_Tencent_OpenSDK_Tencent_Weibo::getAuthorizeURL($token); 获得跳转授权URL
  19. * 6、Services_Tencent_OpenSDK_Tencent_Weibo::getAccessToken($oauth_verifier) 获得access token
  20. * 7、Services_Tencent_OpenSDK_Tencent_Weibo::call();调用API接口
  21. *
  22. * 建议:
  23. * 1、PHP5.2 以下版本,可以使用Pear库中的 Service_JSON 来兼容json_decode
  24. * 2、使用 session_set_save_handler 来重写SESSION。调用API接口前需要主动session_start
  25. * 3、OpenSDK的文件和类名的命名规则符合Pear 和 Zend 规则
  26. * 如果你的代码也符合这样的标准 可以方便的加入到__autoload规则中
  27. *
  28. * @author icehu@vip.qq.com
  29. */
  30. class Services_Tencent_OpenSDK_Tencent_Weibo extends Services_Tencent_OpenSDK_OAuth_Interface
  31. {
  32. private static $accessTokenURL = 'http://open.t.qq.com/cgi-bin/access_token';
  33. private static $authorizeURL = 'http://open.t.qq.com/cgi-bin/authorize';
  34. private static $requestTokenURL = 'http://open.t.qq.com/cgi-bin/request_token';
  35. /**
  36. * OAuth 对象
  37. * @var Services_Tencent_OpenSDK_OAuth_Client
  38. */
  39. protected static $oauth = null;
  40. /**
  41. * OAuth 版本
  42. * @var string
  43. */
  44. protected static $version = '1.0';
  45. /**
  46. * 存储oauth_token的session key
  47. */
  48. const OAUTH_TOKEN = 'tencent_oauth_token';
  49. /**
  50. * 存储oauth_token_secret的session key
  51. */
  52. const OAUTH_TOKEN_SECRET = 'tencent_oauth_token_secret';
  53. /**
  54. * 存储access_token的session key
  55. */
  56. const ACCESS_TOKEN = 'tencent_access_token';
  57. /**
  58. * 存储oauth_name的Session key
  59. */
  60. const OAUTH_NAME = 'tencent_oauth_name';
  61. const OPENID = 'tencent_open_id';
  62. const OPENKEY = 'tencent_open_key';
  63. /**
  64. * 获取requestToken
  65. *
  66. * 返回的数组包括:
  67. * oauth_token:返回的request_token
  68. * oauth_token_secret:返回的request_secret
  69. * oauth_callback_confirmed:回调确认
  70. *
  71. * @param string $callback 回调地址
  72. * @return array
  73. */
  74. public static function getRequestToken($callback='null')
  75. {
  76. self::getOAuth()->setTokenSecret('');
  77. $response = self::request( self::$requestTokenURL, 'GET' , array(
  78. 'oauth_callback' => $callback,
  79. ));
  80. parse_str($response , $rt);
  81. if($rt['oauth_token'] && $rt['oauth_token_secret'])
  82. {
  83. self::getOAuth()->setTokenSecret($rt['oauth_token_secret']);
  84. self::setParam(self::OAUTH_TOKEN, $rt['oauth_token']);
  85. self::setParam(self::OAUTH_TOKEN_SECRET, $rt['oauth_token_secret']);
  86. return $rt;
  87. }
  88. else
  89. {
  90. return false;
  91. }
  92. }
  93. /**
  94. *
  95. * 获得授权URL
  96. *
  97. * @param string|array $token
  98. * @param bool $mini 是否mini窗口
  99. * @return string
  100. */
  101. public static function getAuthorizeURL($token , $mini=false)
  102. {
  103. if(is_array($token))
  104. {
  105. $token = $token['oauth_token'];
  106. }
  107. return self::$authorizeURL . '?oauth_token=' . $token . ($mini ? '&mini=1' : '');
  108. }
  109. /**
  110. * 获得Access Token
  111. * @param string $oauth_verifier
  112. * @return array
  113. */
  114. public static function getAccessToken( $oauth_verifier = false )
  115. {
  116. $response = self::request( self::$accessTokenURL, 'GET' , array(
  117. 'oauth_token' => self::getParam(self::OAUTH_TOKEN),
  118. 'oauth_verifier' => $oauth_verifier
  119. ));
  120. parse_str($response,$rt);
  121. if( $rt['oauth_token'] && $rt['oauth_token_secret'] )
  122. {
  123. self::getOAuth()->setTokenSecret($rt['oauth_token_secret']);
  124. self::setParam(self::ACCESS_TOKEN, $rt['oauth_token']);
  125. self::setParam(self::OAUTH_TOKEN_SECRET, $rt['oauth_token_secret']);
  126. self::setParam(self::OAUTH_NAME, $rt['name']);
  127. self::setParam(self::OPENID, $_GET['openid']);
  128. self::setParam(self::OPENKEY, $_GET['openkey']);
  129. }
  130. return $rt;
  131. }
  132. /**
  133. * 统一调用接口的方法
  134. * 照着官网的参数往里填就行了
  135. * 需要调用哪个就填哪个,如果方法调用得频繁,可以封装更方便的方法。
  136. *
  137. * 如果上传文件 $method = 'POST';
  138. * $multi 是一个二维数组
  139. *
  140. * array(
  141. * '{fieldname}' => array( //第一个文件
  142. * 'type' => 'mine 类型',
  143. * 'name' => 'filename',
  144. * 'data' => 'filedata 字节流',
  145. * ),
  146. * ...如果接受多个文件,可以再加
  147. * )
  148. *
  149. * @param string $command 官方说明中去掉 http://open.t.qq.com/api/ 后面剩余的部分
  150. * @param array $params 官方说明中接受的参数列表,一个关联数组
  151. * @param string $method 官方说明中的 method GET/POST
  152. * @param false|array $multi 是否上传文件
  153. * @param bool $is_oauth 是否使用oauth方式调用,默认为是。如果置为false,则表明是用openid&openkey方式调用
  154. * @param bool $decode 是否对返回的字符串解码成数组
  155. * @param Services_Tencent_OpenSDK_Tencent_Weibo::RETURN_JSON|Services_Tencent_OpenSDK_Tencent_Weibo::RETURN_XML $format 调用格式
  156. */
  157. public static function call($command , $params=array() , $method = 'GET' ,$multi=false ,$is_oauth=true, $decode=true , $format=self::RETURN_JSON)
  158. {
  159. if($format == self::RETURN_XML)
  160. ;
  161. else
  162. $format == self::RETURN_JSON;
  163. $params['format'] = $format;
  164. //去掉空数据
  165. foreach($params as $key => $val)
  166. {
  167. if(strlen($val) == 0)
  168. {
  169. unset($params[$key]);
  170. }
  171. }
  172. $params['oauth_token'] = self::getParam(self::ACCESS_TOKEN);
  173. $response = self::request( 'http://open.t.qq.com/api/'.ltrim($command,'/') , $method, $params, $multi,$is_oauth);
  174. if($decode)
  175. {
  176. if($format == self::RETURN_JSON)
  177. {
  178. return json_decode($response, true);
  179. }
  180. else
  181. {
  182. //parse xml2array later
  183. return $response;
  184. }
  185. }
  186. else
  187. {
  188. return $response;
  189. }
  190. }
  191. /**
  192. * 获得OAuth 对象
  193. * @return Services_Tencent_OpenSDK_OAuth_Client
  194. */
  195. protected static function getOAuth()
  196. {
  197. if( null === self::$oauth )
  198. {
  199. self::$oauth = new Services_Tencent_OpenSDK_OAuth_Client(self::$_appsecret);
  200. $secret = self::getParam(self::OAUTH_TOKEN_SECRET);
  201. if($secret)
  202. {
  203. self::$oauth->setTokenSecret($secret);
  204. }
  205. }
  206. return self::$oauth;
  207. }
  208. /**
  209. *
  210. * OAuth协议请求接口
  211. *
  212. * @param string $url
  213. * @param string $method
  214. * @param array $params
  215. * @param array $multi
  216. * @return string
  217. * @ignore
  218. */
  219. protected static function request($url , $method , $params , $multi=false,$is_oauth=true)
  220. {
  221. if(!self::$_appkey || !self::$_appsecret)
  222. {
  223. exit('app key or app secret not init');
  224. }
  225. if($is_oauth)
  226. {
  227. $params['oauth_nonce'] = md5( mt_rand(1, 100000) . microtime(true) );
  228. $params['oauth_consumer_key'] = self::$_appkey;
  229. $params['oauth_signature_method'] = 'HMAC-SHA1';
  230. $params['oauth_version'] = self::$version;
  231. $params['oauth_timestamp'] = self::getTimestamp();
  232. }
  233. else
  234. {
  235. /*使用openid和openkey来*/
  236. if ($_SESSION[self::OPENID] && $_SESSION[self::OPENKEY]){
  237. $params['appid'] = self::$_appkey;
  238. $params['openid'] = $_SESSION[self::OPENID];
  239. $params['openkey'] = $_SESSION[self::OPENKEY];
  240. $params['clientip'] = "127.0.0.1";
  241. $params['reqtime'] = time();
  242. $params['wbversion'] = '1';
  243. $params['pf'] = 'tapp';
  244. }
  245. }
  246. return self::getOAuth()->request($url, $method, $params, $multi, $is_oauth);
  247. }
  248. }