Skip to content

Commit

Permalink
Merge pull request #74 from unsplash/feature/download-endpoint
Browse files Browse the repository at this point in the history
Update photo->download method to follow new API Guidelines
  • Loading branch information
lukechesser authored Dec 11, 2017
2 parents 1d4ad8f + bf909c3 commit f18274a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ A PHP client for the [Unsplash API](https://unsplash.com/documentation).
- [Official documentation](https://unsplash.com/documentation)
- [Changelog](https://github.com/CrewLabs/Unsplash-PHP/blob/master/CHANGELOG.md)

Every application must abide by the [API Guidelines](https://medium.com/unsplash/unsplash-api-guidelines-28e0216e6daa).
**Note:** Every application must abide by the [API Guidelines](https://medium.com/unsplash/unsplash-api-guidelines-28e0216e6daa). Specifically, remember to [hotlink images](https://medium.com/unsplash/unsplash-api-guidelines-hotlinking-images-6c6b51030d2a) and [trigger a download when appropriate](https://medium.com/unsplash/unsplash-api-guidelines-triggering-a-download-c39b24e99e02).

## Installation

Expand All @@ -27,7 +27,7 @@ Before using, configure the client with your application ID and secret. If you d

Note that if you're just using actions that require the [public permission scope](#permission-scopes), only the `applicationId` is required.

Note that if utmSource is omitted from $credentials a notice will be raised
Note that if utmSource is omitted from $credentials a notice will be raised

```php
Crew\Unsplash\HttpClient::init([
Expand All @@ -38,7 +38,7 @@ Crew\Unsplash\HttpClient::init([
]);
```
### Authorization workflow
To access actions that are non-public (i.e. uploading a photo to a specific account), you'll need a user's permission to access their data.
To access actions that are non-public (i.e. uploading a photo to a specific account), you'll need a user's permission to access their data.

An example of this flow can be found in /examples/oauth-flow.php

Expand Down Expand Up @@ -340,7 +340,7 @@ Update a collection on the user's behalf.

**Arguments**

Argument | Type | Opt/Required | Note
Argument | Type | Opt/Required | Note
---------------|---------|----------------------
`$parameters` | array | Required | The following keys can be set in the array : `title`, `description`, `private`

Expand Down
13 changes: 8 additions & 5 deletions src/Photo.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class Photo extends Endpoint
{
private $photographer;

/**
* Retrieve the a photo object from the ID specified
*
Expand All @@ -21,7 +21,7 @@ class Photo extends Endpoint
public static function find($id)
{
$photo = json_decode(self::get("/photos/{$id}")->getBody(), true);

return new self($photo);
}

Expand Down Expand Up @@ -197,12 +197,15 @@ public function statistics($resolution = 'days', $quantity = 30)
}

/**
* Returns download link for a photo
* @return string - url to download
* Triggers a download for a photo
* Required under API Guidelines
* @return string - full-res photo URL for downloading
*/
public function download()
{
$link = self::get("/photos/{$this->id}/download");
$download_path = parse_url($this->links['download_location'], PHP_URL_PATH);
$download_query = parse_url($this->links['download_location'], PHP_URL_QUERY);
$link = self::get($download_path . "?" . $download_query);
$linkClass = \GuzzleHttp\json_decode($link->getBody());
return $linkClass->url;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/photos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"Connection": "keep-alive",
"Server": "thin"
},
"body": "{\"id\":\"ZUaqqMxtxYk\",\"width\":4608,\"height\":3072,\"color\":\"#5E7285\",\"urls\":{\"regular\":\"https:\/\/unsplash.imgix.net\/photo-1428681756973-d318f055795a?q=75\\u0026fm=jpg\\u0026w=1080\\u0026fit=max\\u0026s=1e42060181854ebad501daf0b25865ee\",\"small\":\"https:\/\/unsplash.imgix.net\/photo-1428681756973-d318f055795a?q=75\\u0026fm=jpg\\u0026w=400\\u0026fit=max\\u0026s=41bfa165f9e35bbaad529ea9a0029a3e\",\"thumb\":\"https:\/\/unsplash.imgix.net\/photo-1428681756973-d318f055795a?q=75\\u0026fm=jpg\\u0026w=200\\u0026fit=max\\u0026s=b223d24e28ba1ced6731e98d46cd5f83\"},\"links\":{\"self\":\"https:\/\/api.unsplash.com:80\/photos\/ZUaqqMxtxYk\",\"html\":\"http:\/\/lvh.me:3000\/photos\/ZUaqqMxtxYk\",\"download\":\"http:\/\/lvh.me:3000\/photos\/ZUaqqMxtxYk\/download\"},\"user\":{\"id\":\"RWFu3buoUS0\",\"username\":\"andy_brunner\",\"name\":\"Andy Brunner\",\"links\":{\"self\":\"https:\/\/api.unsplash.com:80\/users\/andy_brunner\",\"html\":\"http:\/\/lvh.me:3000\/andy_brunner\",\"photos\":\"https:\/\/api.unsplash.com:80\/users\/andy_brunner\/photos\"}}}"
"body": "{\"id\":\"ZUaqqMxtxYk\",\"width\":4608,\"height\":3072,\"color\":\"#5E7285\",\"urls\":{\"regular\":\"https:\/\/unsplash.imgix.net\/photo-1428681756973-d318f055795a?q=75\\u0026fm=jpg\\u0026w=1080\\u0026fit=max\\u0026s=1e42060181854ebad501daf0b25865ee\",\"small\":\"https:\/\/unsplash.imgix.net\/photo-1428681756973-d318f055795a?q=75\\u0026fm=jpg\\u0026w=400\\u0026fit=max\\u0026s=41bfa165f9e35bbaad529ea9a0029a3e\",\"thumb\":\"https:\/\/unsplash.imgix.net\/photo-1428681756973-d318f055795a?q=75\\u0026fm=jpg\\u0026w=200\\u0026fit=max\\u0026s=b223d24e28ba1ced6731e98d46cd5f83\"},\"links\":{\"self\":\"https:\/\/api.unsplash.com:80\/photos\/ZUaqqMxtxYk\",\"html\":\"http:\/\/lvh.me:3000\/photos\/ZUaqqMxtxYk\",\"download\":\"http:\/\/lvh.me:3000\/photos\/ZUaqqMxtxYk\/download\",\"download_location\":\"https:\/\/api.unsplash.com\/photos\/ZUaqqMxtxYk\/download\"},\"user\":{\"id\":\"RWFu3buoUS0\",\"username\":\"andy_brunner\",\"name\":\"Andy Brunner\",\"links\":{\"self\":\"https:\/\/api.unsplash.com:80\/users\/andy_brunner\",\"html\":\"http:\/\/lvh.me:3000\/andy_brunner\",\"photos\":\"https:\/\/api.unsplash.com:80\/users\/andy_brunner\/photos\"}}}"
}
},{
"request": {
Expand Down Expand Up @@ -800,4 +800,4 @@
},
"body": "{\"id\":\"GQcfdBoVB_g\",\"created_at\":\"2015-04-29T09:33:41-04:00\",\"updated_at\":\"2017-04-05T04:37:25-04:00\",\"width\":3008,\"height\":2000,\"color\":\"#A99381\",\"downloads\":5,\"likes\":0,\"liked_by_user\":false,\"exif\":{\"make\":\"Nikon\",\"model\":\"NIKON D40\",\"exposure_time\":\"1\/200\",\"aperture\":\"7.1\",\"focal_length\":\"14\",\"iso\":200},\"current_user_collections\":[],\"urls\":{\"raw\":\"https:\/\/images.unsplash.com\/photo-1430314409749-d01e45472486\",\"full\":\"https:\/\/images.unsplash.com\/photo-1430314409749-d01e45472486?ixlib=rb-0.3.5&q=100&fm=jpg&crop=entropy&cs=tinysrgb&s=b7056b998528f052b26e10a06bf2c5ce\",\"regular\":\"https:\/\/images.unsplash.com\/photo-1430314409749-d01e45472486?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&s=6072239c8a92496268e802d97db4ccf7\",\"small\":\"https:\/\/images.unsplash.com\/photo-1430314409749-d01e45472486?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&s=1705dc81cf04b429315a37af1c0b1354\",\"thumb\":\"https:\/\/images.unsplash.com\/photo-1430314409749-d01e45472486?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=200&fit=max&s=621532727a456e42e30e9bc9f7bc82a5\"},\"categories\":[{\"id\":4,\"title\":\"Nature\",\"photo_count\":54184,\"links\":{\"self\":\"https:\/\/api.unsplash.com\/categories\/4\",\"photos\":\"https:\/\/api.unsplash.com\/categories\/4\/photos\"}}],\"links\":{\"self\":\"https:\/\/api.unsplash.com\/photos\/GQcfdBoVB_g\",\"html\":\"http:\/\/unsplash.com\/photos\/GQcfdBoVB_g\",\"download\":\"http:\/\/unsplash.com\/photos\/GQcfdBoVB_g\/download\",\"download_location\":\"https:\/\/api.unsplash.com\/photos\/GQcfdBoVB_g\/download\"},\"user\":{\"id\":\"76cpHRrA1ZA\",\"updated_at\":\"2017-04-05T04:37:25-04:00\",\"username\":\"hughbertd\",\"name\":\"Hugh Downer\",\"first_name\":\"Hugh\",\"last_name\":\"Downer\",\"portfolio_url\":\"http:\/\/hughbertd.github.io\",\"bio\":\"\",\"location\":null,\"total_likes\":1,\"total_photos\":1,\"total_collections\":1,\"profile_image\":{\"small\":\"https:\/\/images.unsplash.com\/placeholder-avatars\/extra-large.jpg?ixlib=rb-0.3.5&q=80&fm=jpg&crop=faces&cs=tinysrgb&fit=crop&h=32&w=32&s=0ad68f44c4725d5a3fda019bab9d3edc\",\"medium\":\"https:\/\/images.unsplash.com\/placeholder-avatars\/extra-large.jpg?ixlib=rb-0.3.5&q=80&fm=jpg&crop=faces&cs=tinysrgb&fit=crop&h=64&w=64&s=356bd4b76a3d4eb97d63f45b818dd358\",\"large\":\"https:\/\/images.unsplash.com\/placeholder-avatars\/extra-large.jpg?ixlib=rb-0.3.5&q=80&fm=jpg&crop=faces&cs=tinysrgb&fit=crop&h=128&w=128&s=ee8bbf5fb8d6e43aaaa238feae2fe90d\"},\"links\":{\"self\":\"https:\/\/api.unsplash.com\/users\/hughbertd\",\"html\":\"http:\/\/unsplash.com\/@hughbertd\",\"photos\":\"https:\/\/api.unsplash.com\/users\/hughbertd\/photos\",\"likes\":\"https:\/\/api.unsplash.com\/users\/hughbertd\/likes\",\"portfolio\":\"https:\/\/api.unsplash.com\/users\/hughbertd\/portfolio\",\"following\":\"https:\/\/api.unsplash.com\/users\/hughbertd\/following\",\"followers\":\"https:\/\/api.unsplash.com\/users\/hughbertd\/followers\"}}}"
}
}]
}]

0 comments on commit f18274a

Please sign in to comment.