PageRenderTime 23ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/88li/plugin/txweibo/opent.php

http://phpfor.googlecode.com/
PHP | 303 lines | 181 code | 24 blank | 98 comment | 25 complexity | df584ff359c89dc99e3c261d6b861930 MD5 | raw file
  1. <?php
  2. /**
  3. * ???????
  4. * @param
  5. * @return
  6. * @author tuguska
  7. */
  8. require_once 'oauth.php';
  9. class MBOpenTOAuth {
  10. public $host = 'http://open.t.qq.com/';
  11. public $timeout = 30;
  12. public $connectTimeout = 30;
  13. public $sslVerifypeer = FALSE;
  14. public $format = MB_RETURN_FORMAT;
  15. public $decodeJson = TRUE;
  16. public $httpInfo;
  17. public $userAgent = 'oauth test';
  18. public $decode_json = FALSE;
  19. function accessTokenURL() { return 'https://open.t.qq.com/cgi-bin/access_token'; }
  20. function authenticateURL() { return 'http://open.t.qq.com/cgi-bin/authenticate'; }
  21. function authorizeURL() { return 'http://open.t.qq.com/cgi-bin/authorize'; }
  22. function requestTokenURL() { return 'https://open.t.qq.com/cgi-bin/request_token'; }
  23. function lastStatusCode() { return $this->http_status; }
  24. function __construct($consumer_key, $consumer_secret, $oauth_token = NULL, $oauth_token_secret = NULL) {
  25. $this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1();
  26. $this->consumer = new OAuthConsumer($consumer_key, $consumer_secret);
  27. if (!empty($oauth_token) && !empty($oauth_token_secret)) {
  28. $this->token = new OAuthConsumer($oauth_token, $oauth_token_secret);
  29. } else {
  30. $this->token = NULL;
  31. }
  32. }
  33. /**
  34. * oauth?????????
  35. * ???? oauth_token ?oauth_token_secret?key/value??
  36. */
  37. function getRequestToken($oauth_callback = NULL) {
  38. $parameters = array();
  39. if (!empty($oauth_callback)) {
  40. $parameters['oauth_callback'] = $oauth_callback;
  41. }
  42. $request = $this->oAuthRequest($this->requestTokenURL(), 'GET', $parameters);
  43. $token = OAuthUtil::parse_parameters($request);
  44. $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
  45. return $token;
  46. }
  47. /**
  48. * ????url
  49. * @return string
  50. */
  51. function getAuthorizeURL($token, $signInWithWeibo = TRUE , $url='') {
  52. if (is_array($token)) {
  53. $token = $token['oauth_token'];
  54. }
  55. if (empty($signInWithWeibo)) {
  56. return $this->authorizeURL() . "?oauth_token={$token}";
  57. } else {
  58. return $this->authenticateURL() . "?oauth_token={$token}";
  59. }
  60. }
  61. /**
  62. * ????
  63. * Exchange the request token and secret for an access token and
  64. * secret, to sign API calls.
  65. *
  66. * @return array array("oauth_token" => the access token,
  67. * "oauth_token_secret" => the access secret)
  68. */
  69. function getAccessToken($oauth_verifier = FALSE, $oauth_token = false) {
  70. $parameters = array();
  71. if (!empty($oauth_verifier)) {
  72. $parameters['oauth_verifier'] = $oauth_verifier;
  73. }
  74. $request = $this->oAuthRequest($this->accessTokenURL(), 'GET', $parameters);
  75. $token = OAuthUtil::parse_parameters($request);
  76. $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
  77. return $token;
  78. }
  79. function jsonDecode($response, $assoc=true) {
  80. $response = preg_replace('/[^\x20-\xff]*/', "", $response);
  81. $jsonArr = json_decode($response, $assoc);
  82. if(!is_array($jsonArr))
  83. {
  84. throw new Exception('????!');
  85. }
  86. $ret = $jsonArr["ret"];
  87. $msg = $jsonArr["msg"];
  88. /**
  89. *Ret=0 ????
  90. *Ret=1 ????
  91. *Ret=2 ????
  92. *Ret=3 ????
  93. *Ret=4 ???????
  94. */
  95. switch ($ret) {
  96. case 0:
  97. return $jsonArr;;
  98. break;
  99. case 1:
  100. throw new Exception('????!');
  101. break;
  102. case 2:
  103. throw new Exception('????!');
  104. break;
  105. case 3:
  106. throw new Exception('????!');
  107. break;
  108. default:
  109. $errcode = $jsonArr["errcode"];
  110. if(isset($errcode)) //????????
  111. {
  112. throw new Exception("????");
  113. break;
  114. //require_once MB_COMM_DIR.'/api_errcode.class.php';
  115. //$msg = ApiErrCode::getMsg($errcode);
  116. }
  117. throw new Exception('???????!');
  118. break;
  119. }
  120. }
  121. /**
  122. * ?????get??.
  123. * @return mixed
  124. */
  125. function get($url, $parameters) {
  126. $response = $this->oAuthRequest($url, 'GET', $parameters);
  127. if (MB_RETURN_FORMAT === 'json') {
  128. return $this->jsonDecode($response, true);
  129. }
  130. return $response;
  131. }
  132. /**
  133. * ?????post??.
  134. * @return mixed
  135. */
  136. function post($url, $parameters = array() , $multi = false) {
  137. $response = $this->oAuthRequest($url, 'POST', $parameters , $multi );
  138. if (MB_RETURN_FORMAT === 'json') {
  139. return $this->jsonDecode($response, true);
  140. }
  141. return $response;
  142. }
  143. /**
  144. * DELTE wrapper for oAuthReqeust.
  145. * @return mixed
  146. */
  147. function delete($url, $parameters = array()) {
  148. $response = $this->oAuthRequest($url, 'DELETE', $parameters);
  149. if (MB_RETURN_FORMAT === 'json') {
  150. return $this->jsonDecode($response, true);
  151. }
  152. return $response;
  153. }
  154. /**
  155. * ????????
  156. * @return string
  157. */
  158. function oAuthRequest($url, $method, $parameters , $multi = false) {
  159. if (strrpos($url, 'http://') !== 0 && strrpos($url, 'https://') !== 0) {
  160. $url = "{$this->host}{$url}.{$this->format}";
  161. }
  162. $request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $parameters);
  163. $request->sign_request($this->sha1_method, $this->consumer, $this->token);
  164. switch ($method) {
  165. case 'GET':
  166. return $this->http($request->to_url(), 'GET');
  167. default:
  168. return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata($multi) , $multi );
  169. }
  170. }
  171. function http($url, $method, $postfields = NULL , $multi = false){
  172. //$https = 0;
  173. //?????https??
  174. if(strrpos($url, 'https://')===0){
  175. $port = 443;
  176. $version = '1.1';
  177. $host = 'ssl://'.MB_API_HOST;
  178. }else{
  179. $port = 80;
  180. $version = '1.0';
  181. $host = MB_API_HOST;
  182. }
  183. $header = "$method $url HTTP/$version\r\n";
  184. $header .= "Host: ".MB_API_HOST."\r\n";
  185. if($multi){
  186. $header .= "Content-Type: multipart/form-data; boundary=" . OAuthUtil::$boundary . "\r\n";
  187. }else{
  188. $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
  189. }
  190. if(strtolower($method) == 'post' ){
  191. $header .= "Content-Length: ".strlen($postfields)."\r\n";
  192. $header .= "Connection: Close\r\n\r\n";
  193. $header .= $postfields;
  194. }else{
  195. $header .= "Connection: Close\r\n\r\n";
  196. }
  197. $ret = '';
  198. $fp = fsockopen($host,$port,$errno,$errstr,30);
  199. if(!$fp){
  200. $error = '??sock????';
  201. throw new Exception($error);
  202. }else{
  203. fwrite ($fp, $header);
  204. while (!feof($fp)) {
  205. $ret .= fgets($fp, 4096);
  206. }
  207. fclose($fp);
  208. if(strrpos($ret,'Transfer-Encoding: chunked')){
  209. $info = split("\r\n\r\n",$ret);
  210. $response = split("\r\n",$info[1]);
  211. $t = array_slice($response,1,-1);
  212. $returnInfo = implode('',$t);
  213. }else{
  214. $response = split("\r\n\r\n",$ret);
  215. $returnInfo = $response[1];
  216. }
  217. //??utf-8??
  218. return iconv("utf-8","utf-8//ignore",$returnInfo);
  219. }
  220. }
  221. /*
  222. ??curl??????,??????????
  223. function http($url, $method, $postfields = NULL , $multi = false){
  224. $this->http_info = array();
  225. $ci = curl_init();
  226. curl_setopt($ci, CURLOPT_USERAGENT, $this->userAgent);
  227. curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout);
  228. curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
  229. curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
  230. curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->sslVerifypeer);
  231. curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
  232. curl_setopt($ci, CURLOPT_HEADER, FALSE);
  233. switch ($method) {
  234. case 'POST':
  235. curl_setopt($ci, CURLOPT_POST, TRUE);
  236. if (!empty($postfields)) {
  237. curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
  238. }
  239. break;
  240. case 'DELETE':
  241. curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
  242. if (!empty($postfields)) {
  243. $url = "{$url}?{$postfields}";
  244. }
  245. }
  246. $header_array = array();
  247. $header_array2=array();
  248. if( $multi )
  249. $header_array2 = array("Content-Type: multipart/form-data; boundary=" . OAuthUtil::$boundary , "Expect: ");
  250. foreach($header_array as $k => $v)
  251. array_push($header_array2,$k.': '.$v);
  252. curl_setopt($ci, CURLOPT_HTTPHEADER, $header_array2 );
  253. curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE );
  254. curl_setopt($ci, CURLOPT_URL, $url);
  255. $response = curl_exec($ci);
  256. $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
  257. $this->http_info = array_merge($this->http_info, curl_getinfo($ci));
  258. $this->url = $url;
  259. print_r($response);
  260. curl_close ($ci);
  261. return $response;
  262. }*/
  263. function getHeader($ch, $header) {
  264. $i = strpos($header, ':');
  265. if (!empty($i)) {
  266. $key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
  267. $value = trim(substr($header, $i + 2));
  268. $this->http_header[$key] = $value;
  269. }
  270. return strlen($header);
  271. }
  272. }
  273. ?>