OAuth2.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * Provides access to the QIHOO360 Platform. This class provides
  4. * a majority of the functionality needed.
  5. */
  6. class Qihoo_OAuth2
  7. {
  8. private $_clientId = ""; // api key
  9. private $_clientSecret = ""; // app secret
  10. // Set up the API root URL.
  11. const HOST = 'https://openapi.360.cn';
  12. const AUTHORIZE_URL = 'https://openapi.360.cn/oauth2/authorize';
  13. const ACESSTOKEN_URL = 'https://openapi.360.cn/oauth2/access_token';
  14. const SCOPE_BASIC = 'basic';
  15. const REDIRECT_URL = 'oob';
  16. /**
  17. *
  18. * @var Qihoo_Http
  19. */
  20. private $_http;
  21. /**
  22. *
  23. * @var Qihoo_Logger_Base
  24. */
  25. private $_logger = false;
  26. private $_scope;
  27. public function __construct($clientId, $clientSecret, $scope)
  28. {
  29. $this->_http = new Qihoo_Util();
  30. if (empty($clientId)) {
  31. throw new Qihoo_Exception(Qihoo_Exception::CODE_NO_APPKEY);
  32. }
  33. if (empty($clientSecret)) {
  34. throw new Qihoo_Exception(Qihoo_Exception::CODE_NO_SECRET);
  35. }
  36. $this->_clientId = $clientId;
  37. $this->_clientSecret = $clientSecret;
  38. if (empty($scope)) {
  39. $scope = self::SCOPE_BASIC;
  40. }
  41. $this->_scope = $scope;
  42. }
  43. /**
  44. * 通过code同时换取token(包括access_token, fresh_token)和用户信息
  45. * @param String $code
  46. * @return array
  47. */
  48. public function getInfoByCode($code)
  49. {
  50. $token = $this->getAccessTokenByCode($code);
  51. $user = $this->userMe($token['access_token']);
  52. return array(
  53. 'token' => $token,
  54. 'user' => $user,
  55. );
  56. }
  57. /**
  58. * 打开调试
  59. */
  60. public function setLogger($logger)
  61. {
  62. $this->_logger = $logger;
  63. }
  64. /**
  65. * 通过code来换取access_token以及refresh_token
  66. *
  67. * @param string $code Authorized Code get by send HTTP Authorize request.
  68. *
  69. * @return a new access token and refresh token.
  70. */
  71. public function getAccessTokenByCode($code, $redirectUri = null)
  72. {
  73. if ($redirectUri === null) {
  74. $redirectUri = self::REDIRECT_URL;
  75. }
  76. $data = array(
  77. 'grant_type' => "authorization_code",
  78. 'code' => $code,
  79. 'client_id' => $this->_clientId,
  80. 'client_secret' => $this->_clientSecret,
  81. 'redirect_uri' => $redirectUri,
  82. 'scope' => $this->_scope,
  83. );
  84. return $this->_request(self::ACESSTOKEN_URL, $data);
  85. }
  86. /**
  87. * 请求360接口
  88. * @param type $url
  89. * @param type $data
  90. * @param type $decode
  91. * @return type
  92. * @throws Qihoo_Exception
  93. */
  94. private function _request($url, $data, $decode = true)
  95. {
  96. $debugUrl = $url . '?' . http_build_query($data);
  97. $this->_debug(__METHOD__, "请求 $debugUrl");
  98. $jsonStr = Qihoo_Util::request($url, Qihoo_Util::METHOD_GET, $data);
  99. $err = Qihoo_Util::getError();
  100. if ($err) {
  101. $errMsg = "错误:{$err['error']}({$err['errno']})";
  102. $this->_debug(__METHOD__, $errMsg);
  103. throw new Qihoo_Exception(Qihoo_Exception::CODE_NET_ERROR, $errMsg . "\r\n" . $url);
  104. }
  105. if (empty($jsonStr)) {
  106. $this->_debug(__METHOD__, "请求$debugUrl 返回了空字符串");
  107. throw new Qihoo_Exception(Qihoo_Exception::CODE_NET_ERROR, $debugUrl);
  108. }
  109. $this->_debug(__METHOD__, "请求$debugUrl 返回了" . $jsonStr);
  110. if (!$decode) {
  111. return $jsonStr;
  112. }
  113. $response = json_decode($jsonStr, true);
  114. if (empty($response)) {
  115. $this->_debug(__METHOD__, "json_decode失败,原串为$jsonStr");
  116. throw new Qihoo_Exception(Qihoo_Exception::CODE_JSON_ERROR, $jsonStr);
  117. }
  118. $this->_debug(__METHOD__, "请求{$debugUrl} json解压后结果为" . var_export($response, 1));
  119. if (!empty($response['error_code'])) {
  120. $this->_debug(__METHOD__, "返回结果有错误:" . $response['error'] . "($response[error_code])");
  121. throw new Qihoo_Exception($response['error_code'], $response['error']);
  122. }
  123. return $response;
  124. }
  125. /**
  126. * 调试日志
  127. * @param type $location
  128. * @param type $msg
  129. * @return type
  130. */
  131. private function _debug($location, $msg)
  132. {
  133. if (!$this->_logger) {
  134. return;
  135. }
  136. $this->_logger->log($location, $msg);
  137. }
  138. /**
  139. * 使用refresh_token来刷新access_token
  140. *
  141. * @param string $refreshToken A string of refresh token.
  142. * @param string $scope Scope limit.
  143. *
  144. * @return a new access token and refresh token.
  145. */
  146. function getAccessTokenByRefreshToken($refreshToken)
  147. {
  148. $data = array(
  149. 'grant_type' => "refresh_token",
  150. 'refresh_token' => $refreshToken,
  151. 'client_id' => $this->_clientId,
  152. 'client_secret' => $this->_clientSecret,
  153. 'scope' => $this->_scope,
  154. );
  155. return $this->_request(self::ACESSTOKEN_URL, $data);
  156. }
  157. /**
  158. * 获取用户信息
  159. * @param String $tokenStr
  160. * @return array
  161. */
  162. public function userMe($tokenStr)
  163. {
  164. $url = self::HOST;
  165. $url .= '/user/me.json';
  166. $data = array(
  167. 'access_token' => $tokenStr,
  168. );
  169. return $this->_request($url, $data);
  170. }
  171. /**
  172. * 获取token对应的信息,可以用来检查token是否有效
  173. * @param String $tokenStr
  174. * @return array
  175. */
  176. public function getTokenInfo($tokenStr)
  177. {
  178. $data = array(
  179. 'access_token' => $tokenStr,
  180. );
  181. return $this->_request(self::HOST . '/oauth2/get_token_info.json', $data);
  182. }
  183. }