arcasolutions/unsplash

Wrapper to access the Unsplash API and photo library by Arca Solutions

2.4.4 2019-01-16 17:58 UTC

README

Build Status

A PHP client for the Unsplash API.

Quick links to methods you're likely to care about:

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 arcasolutions/unsplash

Usage

Configuration

Before using, configure the client with your application ID and secret. If you don't have an application ID and secret, follow the steps from the Unsplash API to register your application.

Note that if you're just using actions that require the public permission scope, only the applicationId is required.

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

Crew\Unsplash\HttpClient::init([
	'applicationId'	=> 'YOUR APPLICATION ID',
	'secret'		=> 'YOUR APPLICATION SECRET',
	'callbackUrl'	=> 'https://your-application.com/oauth/callback',
	'utmSource' => 'NAME OF YOUR APPLICATION'
]);

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.

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'];
Crew\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:

Crew\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)

API methods

For more information about the 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.

If $returnArrayObject is set to false they return PageResult which contains array of results, total number of elements, page and total number of pages.

There are three exceptions: Search::collections, Search::photos and Search::users always returns PageResult.

Search

Crew\Unsplash\Search::photos($filters)

Retrieve a single page of photo results depending on search results.

Arguments

Example

$filters  = [
    'query' => 'Some query',
    'page' => 3,
    'per_page' => 15,
    'orientation' => 'landscape'
];

Crew\Unsplash\Search::photos($filters);

Crew\Unsplash\Search::collections($filters)

Retrieve a single page of collection results depending on search results.

Arguments

Example

$filters  = [
    'query' => 'Some query',
    'page' => 3,
    'per_page' => 15,
];

Crew\Unsplash\Search::collections($filters);

Crew\Unsplash\Search::users($search, $page, $per_page)

Retrieve a single page of user results depending on search results.

Arguments

Example

$filters  = [
    'query' => 'Some query',
    'page' => 3,
    'per_page' => 15,
];

Crew\Unsplash\Search::users($filters);

Curated Collection

Crew\Unsplash\CuratedCollection::all($filters, $returnArrayObject)

Retrieve the list of curated collections.

Arguments

Example

$filters  = [
    'page' => 3,
    'per_page' => 15,
];

Crew\Unsplash\CuratedCollection::all($page, $per_page);

Example for returning PageResult instead of ArrayObject

Crew\Unsplash\CuratedCollection::all($page, $per_page, false);

Crew\Unsplash\CuratedCollection::find($id)

Retrieve a specific curated collection.

Arguments

Example

Crew\Unsplash\CuratedCollection::find(integer $id);

Crew\Unsplash\CuratedCollection::photos($returnArrayObject)

Retrieve photos from a curated collection.

Note: You need to instantiate a curated collection object first.

Arguments

Example

$collection = Crew\Unsplash\CuratedCollection::find(integer $id);
$photos = $collection->photos();

Example for returning PageResult instead of ArrayObject

$collection = Crew\Unsplash\CuratedCollection::find(integer $id);
$photos = $collection->photos(false);

Collection

Crew\Unsplash\Collection::all($filters, $returnArrayObject)

Retrieve the list of collections.

Arguments

Example

$filters  = [
    'page' => 3,
    'per_page' => 15,
];


Crew\Unsplash\Collection::all($filters);

Example for returning PageResult instead of ArrayObject

Crew\Unsplash\Collection::all($filters, false);

Crew\Unsplash\Collection::featured($filters, $returnArrayObject)

Retrieve list of featured collections.

Arguments

Example

$filters  = [
    'page' => 3,
    'per_page' => 15,
];


Crew\Unsplash\Collection::featured($filters);

Example for returning PageResult instead of ArrayObject

Crew\Unsplash\Collection::featured($filters, false);

Crew\Unsplash\Collection::related($returnArrayObject)

Retrieve list of featured collections.

Note You must instantiate a collection first

Arguments

Example

$collection = Crew\Unsplash\Collection::find($id);
$collection->related();

Example for returning PageResult instead of ArrayObject

$collection = Crew\Unsplash\Collection::find($id);
$collection->related(false);

Crew\Unsplash\Collection::find($id)

Retrieve a specific collection.

Arguments

Example

Crew\Unsplash\Collection::find(integer $id);

Crew\Unsplash\Collection::photos($filters, $returnArrayObject)

Retrieve photos from a collection.

Note: You need to instantiate a collection object first.

Arguments

Example

$filters  = [
    'page' => 3,
    'per_page' => 15,
];

$collection = Crew\Unsplash\Collection::find(integer $id);
$photos = $collection->photos($filters);

Example for returning PageResult instead of ArrayObject

$filters  = [
    'page' => 3,
    'per_page' => 15,
];

$collection = Crew\Unsplash\Collection::find(integer $id);
$photos = $collection->photos($filters, false);

Crew\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 = Crew\Unsplash\Collection::create($title);

Crew\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

Example

$collection = Crew\Unsplash\Collection::find(int $id);
$collection->update(['private' => true])

Crew\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 = Crew\Unsplash\Collection::find(int $id);
$collection->destroy()

Crew\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 = Crew\Unsplash\Collection::find(int $id);
$collection->add(int $photo_id)

Crew\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 = Crew\Unsplash\Collection::find(int $id);
$collection->remove(int $photo_id)

Photo

Crew\Unsplash\Photo::all($filters, $returnArrayObject)

Retrieve a list of photos.

Arguments

Example

$filters  = [
    'page' => 3,
    'per_page' => 15,
    'order_by' => 'latest',
];

Crew\Unsplash\Photo::all($filters);

Example for returning PageResult instead of ArrayObject

$filters  = [
    'page' => 3,
    'per_page' => 15,
    'order_by' => 'latest',
];

Crew\Unsplash\Photo::all($filters, false);

Crew\Unsplash\Photo::curated($filters, $returnArrayObject)

Retrieve a list of curated photos.

Arguments

Example

$filters  = [
    'page' => 3,
    'per_page' => 15,
    'order_by' => 'latest',
];

Crew\Unsplash\Photo::curated($page, $per_page, $order_by);

Example for returning PageResult instead of ArrayObject

$filters  = [
    'page' => 3,
    'per_page' => 15,
    'order_by' => 'latest',
];

Crew\Unsplash\Photo::curated($page, $per_page, $order_by, false);

Crew\Unsplash\Photo::find($id)

Retrieve a specific photo.

Arguments

Example

Crew\Unsplash\Photo::find($id);

Crew\Unsplash\Photo::create($file_path)

Post a photo on the user's behalf.

Note: You need the write_photos permission scope

Arguments

Example

Crew\Unsplash\Photo::create( $file_path);

Crew\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 = Crew\Unsplash\Photo::find(string $id)
$photo->update(array $parameters);

Crew\Unsplash\Photo::photographer()

Retrieve the photo's photographer.

Note: You need to instantiate a photo object first

Arguments

N/A

Example

$photo = Crew\Unsplash\Photo::find(string $id);
$photo->photographer();

Crew\Unsplash\Photo::random($filters)

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 = [
    'featured' => true,
    'username' => 'andy_brunner',
    'query'    => 'coffee',
];

Crew\Unsplash\Photo::random($filters);

Crew\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 = Crew\Unsplash\Photo::find(string $id);
$photo->like();

Crew\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 = Crew\Unsplash\Photo::find(string $id);
$photo->unlike();

Crew\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 = Crew\Unsplash\Photo::find($id);
$photo->statistics('days', 7);

Crew\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 = Crew\Unsplash\Photo::find();
$photo->download();

User

Crew\Unsplash\User::find($username)

Retrieve a user's information.

Arguments

Example

Crew\Unsplash\User::find($username)

Crew\Unsplash\User::portfolio($username)

Retrieve a link to the user's portfolio page.

Arguments

Example

Crew\Unsplash\User::portfolio($username)

Crew\Unsplash\User::current()

Retrieve the user's private information.

Note: You need the read_user permission scope

Arguments

N/A

Example

$user = Crew\Unsplash\User::current();

Crew\Unsplash\User::photos($filters)

Retrieve user's photos.

Note: You need to instantiate a user object first

Arguments

Example

$filters  = [
    'page' => 3,
    'per_page' => 15,
    'order_by' => 'latest',
];

$user = Crew\Unsplash\User::find($username);
$user->photos($filters);

Crew\Unsplash\User::collections($filters)

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

$filters  = [
    'page' => 3,
    'per_page' => 15,
    'order_by' => 'latest',
];

$user = Crew\Unsplash\User::find($username);
$user->collections($filters);

Crew\Unsplash\User::likes($filters)

Retrieve user's collections.

Note: You need to instantiate a user object first

Arguments

Example

$user = Crew\Unsplash\User::find($username);
$user->likes($page, $per_page, $order_by);

Crew\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 = Crew\Unsplash\User::current();
$user->update(['first_name' => 'Elliot', 'last_name' => 'Alderson']);

Crew\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 = Crew\Unsplash\User::find($id);
$user->statistics('days', 7);

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/arcasolutions/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.