YZOauthClient.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. require_once __DIR__ . '/YZHttpClient.php';
  3. class YZOauthClient{
  4. private static $oauthUrl = 'https://open.youzan.com/oauth/token';
  5. public function __construct($client_id, $client_secret, $access_token = NULL, $refresh_token = NULL) {
  6. $this->client_id = $client_id;
  7. $this->client_secret = $client_secret;
  8. $this->access_token = $access_token;
  9. $this->refresh_token = $refresh_token;
  10. }
  11. /**
  12. * 获取access_token
  13. */
  14. public function getToken( $type, $keys ) {
  15. $params = array();
  16. $params['client_id'] = $this->client_id;
  17. $params['client_secret'] = $this->client_secret;
  18. if ( $type === 'token' ) {
  19. $params['grant_type'] = 'refresh_token';
  20. $params['refresh_token'] = $keys['refresh_token'];
  21. } elseif ( $type === 'code' ) {
  22. $params['grant_type'] = 'authorization_code';
  23. $params['code'] = $keys['code'];
  24. $params['redirect_uri'] = $keys['redirect_uri'];
  25. }
  26. return $this->parseResponse(
  27. YZHttpClient::post(self::$oauthUrl, $params)
  28. );
  29. }
  30. private function parseResponse($responseData) {
  31. $data = json_decode($responseData, true);
  32. if (null === $data) throw new Exception('response invalid, data: ' . $responseData);
  33. return $data;
  34. }
  35. }