DuoshuoClient.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. *
  4. * @link http://duoshuo.com/
  5. * @author shen2
  6. *
  7. */
  8. class DuoshuoClient{
  9. var $end_point = 'http://duoshuo.com/api/';
  10. var $format = 'json';
  11. var $userAgent;
  12. var $shortName;
  13. var $secret;
  14. var $accessToken;
  15. var $http;
  16. function __construct($shortName = null, $secret = null, $remoteAuth = null, $accessToken = null){
  17. global $wp_version;
  18. $this->shortName = $shortName;
  19. $this->secret = $secret;
  20. $this->remoteAuth = $remoteAuth;
  21. $this->accessToken = $accessToken;
  22. $this->http = new WP_Http();
  23. $this->userAgent = 'WordPress/' . $wp_version . '|Duoshuo/'. Duoshuo::VERSION;
  24. }
  25. /**
  26. *
  27. * @param $method
  28. * @param $path
  29. * @param $params
  30. * @throws Duoshuo_Exception
  31. * @return array
  32. */
  33. function request($method, $path, $params = array()){
  34. $params['short_name'] = $this->shortName;
  35. $params['remote_auth'] = $this->remoteAuth;
  36. if ($this->accessToken)
  37. $params['access_token'] = $this->accessToken;
  38. $url = $this->end_point . $path. '.' . $this->format;
  39. return $this->httpRequest($url, $method, $params);
  40. }
  41. function httpRequest($url, $method, $params){
  42. $args = array(
  43. 'method' => $method,
  44. 'timeout' => 60,
  45. 'redirection' => 5,
  46. 'httpversion' => '1.0',
  47. 'user-agent' => $this->userAgent,
  48. //'blocking' => true,
  49. 'headers' => array('Expect'=>''),
  50. //'cookies' => array(),
  51. //'compress' => false,
  52. //'decompress' => true,
  53. 'sslverify' => false,
  54. //'stream' => false,
  55. //'filename' => null
  56. );
  57. switch($method){
  58. case 'GET':
  59. $url .= '?' . http_build_query($params, null, '&'); // overwrite arg_separator.output
  60. break;
  61. case 'POST':
  62. $args['body'] = $params; // http类自己会做 http_build_query($params, null, '&') 并指定Content-Type
  63. break;
  64. default:
  65. }
  66. $response = $this->http->request($url, $args);
  67. if (isset($response->errors)){
  68. if (isset($response->errors['http_request_failed'])){
  69. $message = $response->errors['http_request_failed'][0];
  70. if ($message == 'name lookup timed out')
  71. $message = 'DNS解析超时,请重试或检查你的主机的域名解析(DNS)设置。';
  72. elseif (stripos($message, 'Could not open handle for fopen') === 0)
  73. $message = '无法打开fopen句柄,请重试或联系多说管理员。http://duoshuo.com/';
  74. elseif (stripos($message, 'Couldn\'t resolve host') === 0)
  75. $message = '无法解析duoshuo.com域名,请重试或检查你的主机的域名解析(DNS)设置。';
  76. elseif (stripos($message, 'Operation timed out after ') === 0)
  77. $message = '操作超时,请重试或联系多说管理员。http://duoshuo.com/';
  78. throw new Duoshuo_Exception($message, Duoshuo_Exception::REQUEST_TIMED_OUT);
  79. }
  80. else
  81. throw new Duoshuo_Exception('连接服务器失败, 详细信息:' . json_encode($response->errors), Duoshuo_Exception::REQUEST_TIMED_OUT);
  82. }
  83. $json = json_decode($response['body'], true);
  84. return $json === null ? $response['body'] : $json;
  85. }
  86. /**
  87. *
  88. * @param string $type
  89. * @param array $keys
  90. */
  91. function getAccessToken( $type, $keys ) {
  92. $params = array(
  93. 'client_id' => $this->shortName,
  94. 'client_secret' => $this->secret,
  95. );
  96. switch($type){
  97. case 'token':
  98. $params['grant_type'] = 'refresh_token';
  99. $params['refresh_token'] = $keys['refresh_token'];
  100. break;
  101. case 'code':
  102. $params['grant_type'] = 'authorization_code';
  103. $params['code'] = $keys['code'];
  104. $params['redirect_uri'] = $keys['redirect_uri'];
  105. break;
  106. case 'password':
  107. $params['grant_type'] = 'password';
  108. $params['username'] = $keys['username'];
  109. $params['password'] = $keys['password'];
  110. break;
  111. default:
  112. throw new Duoshuo_Exception("wrong auth type");
  113. }
  114. $accessTokenUrl = 'http://api.duoshuo.com/oauth2/access_token';
  115. $response = $this->httpRequest($accessTokenUrl, 'POST', $params);
  116. $token = $response;
  117. if ( is_array($token) && !isset($token['error']) ) {
  118. $this->access_token = $token['access_token'];
  119. if (isset($token['refresh_token'])) // 可能没有refresh_token
  120. $this->refresh_token = $token['refresh_token'];
  121. } else {
  122. var_dump($response);var_dump($params);var_dump($token); // 用来调试
  123. throw new Duoshuo_Exception("get access token failed." . $token['error']);
  124. }
  125. return $token;
  126. }
  127. }