diff --git a/README.md b/README.md index d5cbf88..56dd04d 100644 --- a/README.md +++ b/README.md @@ -216,6 +216,15 @@ This method doesn't allow for setting any photo metadata, but you can do the replacement asynchronously (in which case a 'ticket ID' will be returned). +## User Agent + +If you are using PhpFlickr in an application, it is a good idea to set a custom user agent. +This can be done with the following: + +```php +$flickr->setUserAgent('MyVendor-MyApp/1.0.0'); +``` + ## Proxy server PhpFlickr can be used with a proxy server diff --git a/src/PhpFlickr.php b/src/PhpFlickr.php index dad889d..b2c2a12 100644 --- a/src/PhpFlickr.php +++ b/src/PhpFlickr.php @@ -37,6 +37,9 @@ class PhpFlickr { + /** PhpFlickr version. */ + public const VERSION = '5.1.0'; + /** @param string */ protected $api_key; @@ -71,6 +74,9 @@ class PhpFlickr /** @var TokenStorageInterface */ protected $oauthTokenStorage; + /** @var string The User Agent string to send to the Flickr API. */ + protected $userAgent; + /** * @param string $apiKey * @param string|null $secret @@ -79,6 +85,7 @@ public function __construct(string $apiKey, string $secret = null) { $this->api_key = $apiKey; $this->secret = $secret; + $this->userAgent = 'PhpFlickr/' . self::VERSION . ' https://github.com/samwilson/phpflickr'; } /** @@ -157,6 +164,24 @@ protected function cache($request, $response) return false; } + /** + * Set the User Agent string that will be sent to Flickr. + * Should be of the form `product/product-version comment`. + * @param string $userAgent + */ + public function setUserAgent(string $userAgent): void + { + $this->userAgent = $userAgent; + } + + /** + * Get the User Agent string. + */ + public function getUserAgent(): string + { + return $this->userAgent; + } + /** * Send a POST request to the Flickr API. * @param string $command The API endpoint to call. @@ -178,7 +203,10 @@ public function request($command, $args = array(), $nocache = false) if (!($this->response) || $nocache) { $args = array_filter($args); $oauthService = $this->getOauthService(); - $this->response = $oauthService->requestJson($command, 'POST', $args); + $extraHeaders = [ + 'User-Agent' => $this->getUserAgent(), + ]; + $this->response = $oauthService->requestJson($command, 'POST', $args, $extraHeaders); if (!$nocache) { $this->cache($request, $this->response); } diff --git a/src/Uploader.php b/src/Uploader.php index b9ced7c..233f94e 100644 --- a/src/Uploader.php +++ b/src/Uploader.php @@ -111,6 +111,7 @@ protected function sendFile($filename, $params) curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $args); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curl, CURLOPT_USERAGENT, $this->flickr->getUserAgent()); $response = curl_exec($curl); $this->response = $response; curl_close($curl);