crewlabs / unsplash
Wrapper to access the Unsplash API and photo library
Installs: 126 717
Dependents: 4
Suggesters: 0
Security: 0
Stars: 415
Watchers: 21
Forks: 74
Open Issues: 14
Requires
- php: >=7.3.0
- guzzlehttp/guzzle: ^6.3.0|^7.0
- hughbertd/oauth2-unsplash: >=1.0.3
- league/oauth2-client: >=1.4.2
Requires (Dev)
- mockery/mockery: ~1.4.0
- php-vcr/php-vcr: ~1.4
- php-vcr/phpunit-testlistener-vcr: ~3.1
- phpunit/phpunit: ~9.0
- vlucas/phpdotenv: ~4.1.4
- dev-master
- 3.2.1
- 3.2.0
- 3.1.0
- 3.0.0
- 2.5.1
- 2.5.0
- 2.4.4
- 2.4.3
- 2.4.2
- 2.4.1
- 2.4.0
- 2.3.4
- 2.3.3
- 2.3.2
- 2.3.1
- 2.3.0
- 2.2.1
- 2.2.0
- 2.1.0
- 2.0.0
- 1.2.0
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- dev-dependabot/composer/guzzlehttp/psr7-1.9.1
- dev-dependabot/composer/guzzlehttp/guzzle-6.5.8
- dev-update-oauth2
- dev-hugh-use-oauth-package-merge
This package is not auto-updated.
Last update: 2024-11-09 19:23:29 UTC
README
A PHP client for the Unsplash API.
Quick links to methods you're likely to care about:
- Get a list of new photos 🎉
- Get a random photo 🎑
- Trigger a photo download 📡
- Search for a photo by keyword 🕵️♂️
Note: Every application must abide by the API Guidelines. Specifically, remember to hotlink images and trigger a download when appropriate.
Installation
unsplash-php
uses Composer. To use it, require the library
composer require unsplash/unsplash
Usage
Configuration
Before using, configure the client with your access key and secret. If you don't have an access key and secret, follow the steps from the Unsplash API to register your application.
Note: if you're just using actions that require the public permission scope, only the access key is required. Access key is entered as applicationId
due to legacy reasons.
Note: if utmSource is omitted from $credentials a notice will be raised.
Unsplash\HttpClient::init([ 'applicationId' => 'YOUR ACCESS KEY', 'secret' => 'YOUR APPLICATION SECRET', 'callbackUrl' => 'https://your-application.com/oauth/callback', 'utmSource' => 'NAME OF YOUR APPLICATION' ]);
User Authorization workflow
If you need to access actions that are non-public on behalf of the user (i.e. uploading a photo to a specific account), you'll need to follow the user authentication workflow to access their data.
An example of this flow can be found in /examples/oauth-flow.php
Direct them to an authorization URL (configuring any scopes before generating the authorization URL):
$scopes = ['public', 'write_user']; Unsplash\HttpClient::$connection->getConnectionUrl($scopes);
Upon authorization, Unsplash will return to you an authentication code via your OAuth callback handler. Use it to generate an access token:
Unsplash\HttpClient::$connection->generateToken($code);
With the token you can now access any additional non-public actions available for the authorized user.
Permission Scopes
The current permission scopes defined by the Unsplash API are:
public
(Access a user's public data)read_user
(Access a user's private data)write_user
(Edit and create user data)read_photos
(Access private information from a user's photos)write_photos
(Post and edit photos for a user)write_likes
(Like a photo for a user)read_collections
(View a user’s private collections)write_collections
(Create and update a user’s collections)
API methods
For more information about the responses for each call, refer to the official documentation.
Some parameters are identical across all methods:
Note: The methods that return multiple objects return an ArrayObject
, which acts like a normal stdClass.
Search
Photos
Retrieve a single page of photo results depending on search results.
Arguments
Example
$search = 'forest'; $page = 3; $per_page = 15; $orientation = 'landscape'; Unsplash\Search::photos($search, $page, $per_page, $orientation);
Collections
Retrieve a single page of collection results depending on search results.
Arguments
Example
Unsplash\Search::collections($search, $page, $per_page);
Users
Retrieve a single page of user results depending on search results.
Arguments
Example
Unsplash\Search::users($search, $page, $per_page);
Collections
Retrieve the list of collections.
Arguments
Example
Unsplash\Collection::all($page, $per_page);
Unsplash\Collection::photos($page, $per_page)
Retrieve photos from a collection.
Note: You need to instantiate a collection object first.
Arguments
Example
$collection = Unsplash\Collection::find(integer $id); $photos = $collection->photos($page, $per_page);
Unsplash\Collection::related($page, $per_page)
Retrieve list of featured collections.
Note You must instantiate a collection first
Arguments
Example
$collection = Unsplash\Collection::find($id); $collection->related();
Unsplash\Collection::create($title, $description, $private)
Create a collection on the user's behalf.
Note: You need the write_collections
permission scope
Arguments
Example
$collection = Unsplash\Collection::create($title);
Unsplash\Collection::update($parameters)
Update a collection on the user's behalf.
Note: You need to instantiate a collection object first
Note: You need the write_collections
permission scope
Arguments
Argument | Type | Opt/Required | Note
---------------|---------|----------------------
$parameters
| array | Required | The following keys can be set in the array : title
, description
, private
Example
$collection = Unsplash\Collection::find(int $id); $collection->update(['private' => true])
Unsplash\Collection::destroy()
Delete a collection on the user's behalf.
Note: You need to instantiate a collection object first
Note: You need the write_collections
permission scope
Example
$collection = Unsplash\Collection::find(int $id); $collection->destroy()
Unsplash\Collection::add($photo_id)
Add a photo in the collection on the user's behalf.
Note: You need to instantiate a collection object first
Note: You need the write_collections
permission scope
Arguments
Example
$collection = Unsplash\Collection::find(int $id); $collection->add(int $photo_id)
Unsplash\Collection::remove($photo_id)
Remove a photo from the collection on the user's behalf.
Note: You need to instantiate a collection object first
Note: You need the write_collections
permission scope
Arguments
Example
$collection = Unsplash\Collection::find(int $id); $collection->remove(int $photo_id)
Photo
Unsplash\Photo::all($page, $per_page, $order_by)
Retrieve a list of photos.
Arguments
Example
Unsplash\Photo::all($page, $per_page, $order_by);
Unsplash\Photo::find($id)
Retrieve a specific photo.
Arguments
Example
Unsplash\Photo::find($id);
Unsplash\Photo::update($parameters = [])
Post a photo on the user's behalf.
Note: You need the write_photos
permission scope
You need to instantiate the Photo object first
Arguments
Example
$photo = Unsplash\Photo::find(string $id) $photo->update(array $parameters);
Unsplash\Photo::photographer()
Retrieve the photo's photographer.
Note: You need to instantiate a photo object first
Arguments
N/A
Example
$photo = Unsplash\Photo::find(string $id); $photo->photographer();
Unsplash\Photo::random([featured => $value, username => $value, query => $value, w => $value, h => $value])
Retrieve a random photo from specified filters. For more information regarding filtering, refer to the Offical documentation.
Note: An array needs to be passed as a parameter.
Arguments
Example
// Or apply some optional filters by passing a key value array of filters $filters = [ 'username' => 'andy_brunner', 'query' => 'coffee', 'w' => 100, 'h' => 100 ]; Unsplash\Photo::random($filters);
Unsplash\Photo::like()
Like a photo on the user's behalf.
Note: You need to instantiate a photo object first
Note: You need the like_photos
permission scope
Arguments
N/A
Example
$photo = Unsplash\Photo::find(string $id); $photo->like();
Unsplash\Photo::unlike()
Unlike a photo on the user's behalf.
Note: You need to instantiate a photo object first
Note: You need the like_photos
permission scope
Arguments
N/A
Example
$photo = Unsplash\Photo::find(string $id); $photo->unlike();
Unsplash\Photo::statistics(string $resolution, int $quantity)
Retrieve total number of downloads, views and likes of a single photo, as well as the historical breakdown of these stats in a specific timeframe (default is 30 days).
Note: You must instantiate a Photo object first
Arguments
Example
$photo = Unsplash\Photo::find($id); $photo->statistics('days', 7);
Unsplash\Photo::download()
Trigger a download for a photo. This is needed to follow the 'trigger a download' API Guideline.
Note: You must instantiate a Photo object first
Arguments
Example
$photo = Unsplash\Photo::find(); $photo->download();
User
Unsplash\User::find($username)
Retrieve a user's information.
Arguments
Example
Unsplash\User::find($username)
Unsplash\User::portfolio($username)
Retrieve a link to the user's portfolio page.
Arguments
Example
Unsplash\User::portfolio($username)
Unsplash\User::current()
Retrieve the user's private information.
Note: You need the read_user permission scope
Arguments
N/A
Example
$user = Unsplash\User::current();
Unsplash\User::photos($page, $per_page, $order_by)
Retrieve user's photos.
Note: You need to instantiate a user object first
Arguments
Example
$user = Unsplash\User::find($username); $user->photos($page, $per_page);
Unsplash\User::collections($page, $per_page)
Retrieve user's collections.
Note: You need to instantiate a user object first Note: You need the read_collections permission scope to retrieve user's private collections
Arguments
Example
$user = Unsplash\User::find($username); $user->collections($page, $per_page);
Unsplash\User::likes($page, $per_page, $order_by)
Retrieve user's collections.
Note: You need to instantiate a user object first
Arguments
Example
$user = Unsplash\User::find($username); $user->likes($page, $per_page, $order_by);
Unsplash\User::update([$key => value])
Update current user's fields. Multiple fields can be passed in the array.
Note: You need to instantiate a user object first
Note: You need the write_user permission scope.
Arguments
$user = Unsplash\User::current(); $user->update(['first_name' => 'Elliot', 'last_name' => 'Alderson']);
Unsplash\User::statistics(string $resolution, int $quantity)
Retrieve total number of downloads, views and likes for a user, as well as the historical breakdown of these stats in a specific timeframe (default is 30 days).
Note: You must instantiate the User object first
Arguments
Example
$user = Unsplash\User::find($id); $user->statistics('days', 7);
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/unsplash/unsplash-php. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.