Flickr.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace OAuth\OAuth1\Service;
  3. use OAuth\OAuth1\Signature\SignatureInterface;
  4. use OAuth\OAuth1\Token\StdOAuth1Token;
  5. use OAuth\Common\Http\Exception\TokenResponseException;
  6. use OAuth\Common\Http\Uri\Uri;
  7. use OAuth\Common\Consumer\CredentialsInterface;
  8. use OAuth\Common\Http\Uri\UriInterface;
  9. use OAuth\Common\Storage\TokenStorageInterface;
  10. use OAuth\Common\Http\Client\ClientInterface;
  11. class Flickr extends AbstractService
  12. {
  13. protected $format;
  14. public function __construct(
  15. CredentialsInterface $credentials,
  16. ClientInterface $httpClient,
  17. TokenStorageInterface $storage,
  18. SignatureInterface $signature,
  19. UriInterface $baseApiUri = null
  20. ) {
  21. parent::__construct($credentials, $httpClient, $storage, $signature, $baseApiUri);
  22. if ($baseApiUri === null) {
  23. $this->baseApiUri = new Uri('https://api.flickr.com/services/rest/');
  24. }
  25. }
  26. public function getRequestTokenEndpoint()
  27. {
  28. return new Uri('https://www.flickr.com/services/oauth/request_token');
  29. }
  30. public function getAuthorizationEndpoint()
  31. {
  32. return new Uri('https://www.flickr.com/services/oauth/authorize');
  33. }
  34. public function getAccessTokenEndpoint()
  35. {
  36. return new Uri('https://www.flickr.com/services/oauth/access_token');
  37. }
  38. protected function parseRequestTokenResponse($responseBody)
  39. {
  40. parse_str($responseBody, $data);
  41. if (null === $data || !is_array($data)) {
  42. throw new TokenResponseException('Unable to parse response.');
  43. } elseif (!isset($data['oauth_callback_confirmed']) || $data['oauth_callback_confirmed'] != 'true') {
  44. throw new TokenResponseException('Error in retrieving token.');
  45. }
  46. return $this->parseAccessTokenResponse($responseBody);
  47. }
  48. protected function parseAccessTokenResponse($responseBody)
  49. {
  50. parse_str($responseBody, $data);
  51. if ($data === null || !is_array($data)) {
  52. throw new TokenResponseException('Unable to parse response.');
  53. } elseif (isset($data['error'])) {
  54. throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
  55. }
  56. $token = new StdOAuth1Token();
  57. $token->setRequestToken($data['oauth_token']);
  58. $token->setRequestTokenSecret($data['oauth_token_secret']);
  59. $token->setAccessToken($data['oauth_token']);
  60. $token->setAccessTokenSecret($data['oauth_token_secret']);
  61. $token->setEndOfLife(StdOAuth1Token::EOL_NEVER_EXPIRES);
  62. unset($data['oauth_token'], $data['oauth_token_secret']);
  63. $token->setExtraParams($data);
  64. return $token;
  65. }
  66. public function request($path, $method = 'GET', $body = null, array $extraHeaders = array())
  67. {
  68. $uri = $this->determineRequestUriFromPath('/', $this->baseApiUri);
  69. $uri->addToQuery('method', $path);
  70. if (!empty($this->format)) {
  71. $uri->addToQuery('format', $this->format);
  72. if ($this->format === 'json') {
  73. $uri->addToQuery('nojsoncallback', 1);
  74. }
  75. }
  76. $token = $this->storage->retrieveAccessToken($this->service());
  77. $extraHeaders = array_merge($this->getExtraApiHeaders(), $extraHeaders);
  78. $authorizationHeader = array(
  79. 'Authorization' => $this->buildAuthorizationHeaderForAPIRequest($method, $uri, $token, $body)
  80. );
  81. $headers = array_merge($authorizationHeader, $extraHeaders);
  82. return $this->httpClient->retrieveResponse($uri, $body, $headers, $method);
  83. }
  84. public function requestRest($path, $method = 'GET', $body = null, array $extraHeaders = array())
  85. {
  86. return $this->request($path, $method, $body, $extraHeaders);
  87. }
  88. public function requestXmlrpc($path, $method = 'GET', $body = null, array $extraHeaders = array())
  89. {
  90. $this->format = 'xmlrpc';
  91. return $this->request($path, $method, $body, $extraHeaders);
  92. }
  93. public function requestSoap($path, $method = 'GET', $body = null, array $extraHeaders = array())
  94. {
  95. $this->format = 'soap';
  96. return $this->request($path, $method, $body, $extraHeaders);
  97. }
  98. public function requestJson($path, $method = 'GET', $body = null, array $extraHeaders = array())
  99. {
  100. $this->format = 'json';
  101. return $this->request($path, $method, $body, $extraHeaders);
  102. }
  103. public function requestPhp($path, $method = 'GET', $body = null, array $extraHeaders = array())
  104. {
  105. $this->format = 'php_serial';
  106. return $this->request($path, $method, $body, $extraHeaders);
  107. }
  108. }