Skip to content

Commit

Permalink
Add PhpFlickr::setUserAgent() and send User Agent to API
Browse files Browse the repository at this point in the history
Flickr is soon to make the User Agent header required for all
API requests.

Refs: #59
  • Loading branch information
samwilson authored Apr 26, 2023
1 parent 5a2c3e5 commit 4f98fde
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 29 additions & 1 deletion src/PhpFlickr.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@

class PhpFlickr
{
/** PhpFlickr version. */
public const VERSION = '5.1.0';

/** @param string */
protected $api_key;

Expand Down Expand Up @@ -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
Expand All @@ -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';
}

/**
Expand Down Expand Up @@ -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.
Expand All @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions src/Uploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 4f98fde

Please sign in to comment.