2017 © Pedro Peláez
 

library unsplash

Wrapper to access the Unsplash API and photo library

image

unsplash/unsplash

Wrapper to access the Unsplash API and photo library

  • Friday, March 30, 2018
  • by lukechesser
  • Repository
  • 22 Watchers
  • 185 Stars
  • 1,906 Installations
  • PHP
  • 6 Dependents
  • 0 Suggesters
  • 29 Forks
  • 3 Open issues
  • 25 Versions
  • 144 % Grown

The README.md

PHP Unsplash Wrapper

Build Status, (*1)

A PHP client for the Unsplash API., (*2)

Quick links to methods you're likely to care about:, (*3)

Note: Every application must abide by the API Guidelines. Specifically, remember to hotlink images and trigger a download when appropriate., (*4)

Installation

unsplash-php uses Composer. To use it, require the library, (*5)

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., (*6)

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., (*7)

Note: if utmSource is omitted from $credentials a notice will be raised., (*8)

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., (*9)

An example of this flow can be found in /examples/oauth-flow.php, (*10)

Direct them to an authorization URL (configuring any scopes before generating the authorization URL):, (*11)

$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:, (*12)

Unsplash\HttpClient::$connection->generateToken($code);

With the token you can now access any additional non-public actions available for the authorized user., (*13)

Permission Scopes

The current permission scopes defined by the Unsplash API are:, (*14)

  • 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., (*15)

Some parameters are identical across all methods:, (*16)

param Description
$per_page Defines the number of objects per page. Default 10
$page Defines the offset page. Default 1

Note: The methods that return multiple objects return an ArrayObject, which acts like a normal stdClass., (*17)


Photos

Retrieve a single page of photo results depending on search results., (*18)

Arguments, (*19)

Argument Type Opt/Required
$search string Required
$page int Opt (Default: 1)
$per_page int Opt (Default: 10 / Maximum: 30)
$orientation string Opt (Default: null / Available: "landscape", "portrait", "squarish")
$collections string Opt (Default: null / If multiple, comma-separated)
$order_by string How to sort the photos. (Optional; default: relevant). Valid values are latest and relevant.

Example, (*20)

$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., (*21)

Arguments, (*22)

Argument Type Opt/Required
$search string Required
$per_page int Opt (Default: 10 / Maximum: 30)
$page int Opt (Default: 1)

Example, (*23)

Unsplash\Search::collections($search, $page, $per_page);

Users

Retrieve a single page of user results depending on search results., (*24)

Arguments, (*25)

Argument Type Opt/Required
$search string Required
$per_page int Opt (Default: 10 / Maximum: 30)
$page int Opt (Default: 1)

Example, (*26)

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

Collections

#

Retrieve the list of collections., (*27)

Arguments, (*28)

Argument Type Opt/Required
$per_page int Opt (Default: 10 / Maximum: 30)
$page int Opt (Default: 1)

Example, (*29)

Unsplash\Collection::all($page, $per_page);

Unsplash\Collection::photos($page, $per_page)

Retrieve photos from a collection., (*30)

Note: You need to instantiate a collection object first., (*31)

Arguments, (*32)

Argument Type Opt/Required
$per_page int Opt (Default: 10 / Maximum: 30)
$page int Opt (Default: 1)

Example, (*33)

$collection = Unsplash\Collection::find(integer $id);
$photos = $collection->photos($page, $per_page);

Unsplash\Collection::related($page, $per_page)

Retrieve list of featured collections., (*34)

Note You must instantiate a collection first, (*35)

Arguments, (*36)

Argument Type Opt/Required

Example, (*37)

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

Unsplash\Collection::create($title, $description, $private)

Create a collection on the user's behalf., (*38)

Note: You need the write_collections permission scope, (*39)

Arguments, (*40)

Argument Type Opt/Required
$title string Required
$description string Opt (Default: '')
$private boolean Opt (Default: false)

Example, (*41)

$collection = Unsplash\Collection::create($title);

Unsplash\Collection::update($parameters)

Update a collection on the user's behalf., (*42)

Note: You need to instantiate a collection object first, (*43)

Note: You need the write_collections permission scope, (*44)

Arguments, (*45)

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

Example, (*46)

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

Unsplash\Collection::destroy()

Delete a collection on the user's behalf., (*47)

Note: You need to instantiate a collection object first, (*48)

Note: You need the write_collections permission scope, (*49)

Example, (*50)

$collection = Unsplash\Collection::find(int $id);
$collection->destroy()

Unsplash\Collection::add($photo_id)

Add a photo in the collection on the user's behalf., (*51)

Note: You need to instantiate a collection object first, (*52)

Note: You need the write_collections permission scope, (*53)

Arguments, (*54)

Argument Type Opt/Required
$photo_id integer Required

Example, (*55)

$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., (*56)

Note: You need to instantiate a collection object first, (*57)

Note: You need the write_collections permission scope, (*58)

Arguments, (*59)

Argument Type Opt/Required
$photo_id integer Required

Example, (*60)

$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., (*61)

Arguments, (*62)

Argument Type Opt/Required
$per_page int Opt (Default: 10 / Maximum: 30)
$page int Opt (Default: 1)
$order_by string Opt (Default: latest / Available: oldest, popular)

Example, (*63)

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

Unsplash\Photo::find($id)

Retrieve a specific photo., (*64)

Arguments, (*65)

Argument Type Opt/Required
$id int Required

Example, (*66)

Unsplash\Photo::find($id);

Unsplash\Photo::update($parameters = [])

Post a photo on the user's behalf., (*67)

Note: You need the write_photos permission scope You need to instantiate the Photo object first, (*68)

Arguments, (*69)

Argument Type Opt/Required
$parameters array Required

Example, (*70)

$photo = Unsplash\Photo::find(string $id)
$photo->update(array $parameters);

Unsplash\Photo::photographer()

Retrieve the photo's photographer., (*71)

Note: You need to instantiate a photo object first, (*72)

Arguments, (*73)

N/A, (*74)

Example, (*75)

$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., (*76)

Note: An array needs to be passed as a parameter., (*77)

Arguments, (*78)

Argument Type Opt/Required
featured boolean Opt (Limit selection to featured photos)
username string Opt (Limit selection to a single user)
query string Opt (Limit selection to photos matching a search term)
w int Opt (Image width in pixels)
h int Opt (Image height in pixels)

Example, (*79)


// 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., (*80)

Note: You need to instantiate a photo object first, (*81)

Note: You need the like_photos permission scope, (*82)

Arguments, (*83)

N/A, (*84)

Example, (*85)

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

Unsplash\Photo::unlike()

Unlike a photo on the user's behalf., (*86)

Note: You need to instantiate a photo object first, (*87)

Note: You need the like_photos permission scope, (*88)

Arguments, (*89)

N/A, (*90)

Example, (*91)

$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)., (*92)

Note: You must instantiate a Photo object first, (*93)

Arguments, (*94)

Argument Type Opt/Required
resolution string Opt (Accepts only days currently)
quantity int Opt (Defaults to 30, can be between 1 and 30)

Example, (*95)



$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., (*96)

Note: You must instantiate a Photo object first, (*97)

Arguments, (*98)

Argument Type Opt/Required

Example, (*99)

$photo = Unsplash\Photo::find();
$photo->download();

User

Unsplash\User::find($username)

Retrieve a user's information., (*100)

Arguments, (*101)

Argument Type Opt/Required
$username string Required

Example, (*102)

Unsplash\User::find($username)

Unsplash\User::portfolio($username)

Retrieve a link to the user's portfolio page., (*103)

Arguments, (*104)

Argument Type Opt/Required
$username string Required

Example, (*105)

Unsplash\User::portfolio($username)

Unsplash\User::current()

Retrieve the user's private information., (*106)

Note: You need the read_user permission scope, (*107)

Arguments, (*108)

N/A, (*109)

Example, (*110)

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

Unsplash\User::photos($page, $per_page, $order_by)

Retrieve user's photos., (*111)

Note: You need to instantiate a user object first, (*112)

Arguments, (*113)

Argument Type Opt/Required
$per_page int Opt (Default: 10 / Maximum: 30)
$page int Opt (Default: 1)
$order_by string Opt (Default: latest / Available: oldest, popular)

Example, (*114)

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

Unsplash\User::collections($page, $per_page)

Retrieve user's collections., (*115)

Note: You need to instantiate a user object first Note: You need the read_collections permission scope to retrieve user's private collections, (*116)

Arguments, (*117)

Argument Type Opt/Required
$per_page int Opt (Default: 10 / Maximum: 30)
$page int Opt (Default: 1)

Example, (*118)

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

Unsplash\User::likes($page, $per_page, $order_by)

Retrieve user's collections., (*119)

Note: You need to instantiate a user object first, (*120)

Arguments, (*121)

Argument Type Opt/Required
$per_page int Opt (Default: 10 / Maximum: 30)
$page int Opt (Default: 1)
$order_by string Opt (Default: latest / Available: oldest, popular)

Example, (*122)

$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., (*123)

Note: You need to instantiate a user object first, (*124)

Note: You need the write_user permission scope., (*125)

Arguments, (*126)

Argument Type Opt/Required Note
$key string Required The following keys are accepted: username, first_name, last_name, email, url, location, bio, instagram_username
$value mixed required
$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)., (*127)

Note: You must instantiate the User object first, (*128)

Arguments, (*129)

Argument Type Opt/Required
resolution string Opt (Accepts only days currently)
quantity int Opt (Defaults to 30, can be between 1 and 30)

Example, (*130)

$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., (*131)

The Versions

30/03 2018

dev-master

9999999-dev

Wrapper to access the Unsplash API and photo library

  Sources   Download

MIT

The Requires

 

The Development Requires

by Charles Lalonde
by Hugh Downer
by Aaron Klaassen
by Luke Chesser

30/03 2018

2.4.3

2.4.3.0

Wrapper to access the Unsplash API and photo library

  Sources   Download

MIT

The Requires

 

The Development Requires

by Charles Lalonde
by Hugh Downer
by Aaron Klaassen
by Luke Chesser

28/02 2018

2.4.2

2.4.2.0

Wrapper to access the Unsplash API and photo library

  Sources   Download

MIT

The Requires

 

The Development Requires

by Charles Lalonde
by Hugh Downer
by Aaron Klaassen
by Luke Chesser

06/10 2017
06/04 2017

dev-hugh-use-oauth-package-merge

dev-hugh-use-oauth-package-merge

Wrapper to access information from Unsplash API more easily

  Sources   Download

CC0

The Requires

 

The Development Requires

by Charles Lalonde

05/04 2016

1.2.0

1.2.0.0

Wrapper to access information from Unsplash API more easily

  Sources   Download

CC0

The Requires

 

The Development Requires

by Charles Lalonde

24/02 2016

1.1.2

1.1.2.0

Wrapper to access information from Unsplash API more easily

  Sources   Download

CC0

The Requires

 

The Development Requires

by Charles Lalonde

22/02 2016

1.1.1

1.1.1.0

Wrapper to access information from Unsplash API more easily

  Sources   Download

CC0

The Requires

 

The Development Requires

by Charles Lalonde

09/02 2016

1.1.0

1.1.0.0

Wrapper to access information from Unsplash API more easily

  Sources   Download

CC0

The Requires

 

The Development Requires

by Charles Lalonde

20/10 2015

1.0.4

1.0.4.0

Wrapper to access information from Unsplash API more easily

  Sources   Download

CC0

The Requires

 

The Development Requires

by Charles Lalonde

22/09 2015

1.0.3

1.0.3.0

Wrapper to access information from Unsplash API more easily

  Sources   Download

CC0

The Requires

 

The Development Requires

by Charles Lalonde

03/09 2015

1.0.2

1.0.2.0

Wrapper to access information from Unsplash API more easily

  Sources   Download

CC0

The Requires

 

The Development Requires

by Charles Lalonde

26/08 2015

1.0.1

1.0.1.0

Wrapper to access information from Unsplash API more easily

  Sources   Download

CC0

The Requires

 

The Development Requires

by Charles Lalonde

31/07 2015

1.0.0

1.0.0.0

Wrapper to access information from Unsplash API more easily

  Sources   Download

CC0

The Requires

 

The Development Requires

by Charles Lalonde