moodpaper/unsplash

访问 Unsplash API 和照片库的包装器

3.1.1 2021-08-01 11:53 UTC

README

Build Status

Unsplash API 的 PHP 客户端。

快速链接到您可能关心的方法

注意: 每个应用程序都必须遵守 API 指南。具体来说,请记住 禁止图片热链 和在适当的时候 触发下载

安装

unsplash-php 使用 Composer。要使用它,请要求使用库

composer require unsplash/unsplash

使用方法

配置

在使用之前,使用您的访问密钥和密钥配置客户端。如果您没有访问密钥和密钥,请按照 Unsplash API 中的步骤注册您的应用程序。

注意:如果您只是使用需要 公共权限范围 的操作,则只需要访问密钥。由于历史原因,访问密钥以 applicationId 的形式输入。

注意:如果从 $credentials 中省略了 utmSource,则将引发一个警告。

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

用户授权工作流程

如果您需要代表用户访问非公共操作(例如,将照片上传到特定帐户),则需要遵循 用户身份验证工作流程 来访问他们的数据。

此流的一个示例可以在 /examples/oauth-flow.php 中找到

将他们导向授权 URL(在生成授权 URL 之前配置任何范围)

$scopes = ['public', 'write_user'];
Unsplash\HttpClient::$connection->getConnectionUrl($scopes);

在授权后,Unsplash 将通过您的 OAuth 回调处理程序返回一个身份验证代码。使用它来生成访问令牌

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

有了令牌,您现在可以访问授权用户可用的任何其他非公共操作。

权限范围

Unsplash API 当前定义的权限范围是

  • public(访问用户的公共数据)
  • read_user(访问用户的私有数据)
  • write_user(编辑和创建用户数据)
  • read_photos(访问用户照片的私有信息)
  • write_photos(为用户发布和编辑照片)
  • write_likes(为用户点赞照片)
  • read_collections(查看用户的私有收藏)
  • write_collections(创建和更新用户的收藏)

API 方法

有关每个调用响应的更多信息,请参阅 官方文档

一些参数在所有方法中相同

注意:返回多个对象的方法返回一个 ArrayObject,它类似于正常的 stdClass

搜索

照片

根据搜索结果检索单个页面照片结果。

参数

示例

$search = 'forest';
$page = 3;
$per_page = 15;
$orientation = 'landscape';

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

集合

根据搜索结果检索集合的单页结果。

参数

示例

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

用户

根据搜索结果检索用户单页结果。

参数

示例

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

集合

检索集合列表。

参数

示例

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

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

从集合中检索照片。

注意:您需要首先实例化一个集合对象。

参数

示例

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

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

检索精选集合列表。

注意:您必须首先实例化一个集合。

参数

示例

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

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

代表用户创建一个集合。

注意:您需要write_collections权限范围。

参数

示例

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

Unsplash\Collection::update($parameters)

代表用户更新一个集合。

注意:您需要首先实例化一个集合对象。

注意:您需要write_collections权限范围。

参数

参数 | 类型 | 可选/必需 | 备注 ---------------|---------|---------------------- $parameters | array | 必需 | 可以在数组中设置以下键:titledescriptionprivate

示例

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

Unsplash\Collection::destroy()

代表用户删除一个集合。

注意:您需要首先实例化一个集合对象。

注意:您需要write_collections权限范围。

示例

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

Unsplash\Collection::add($photo_id)

代表用户在集合中添加照片。

注意:您需要首先实例化一个集合对象。

注意:您需要write_collections权限范围。

参数

示例

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

Unsplash\Collection::remove($photo_id)

代表用户从集合中移除照片。

注意:您需要首先实例化一个集合对象。

注意:您需要write_collections权限范围。

参数

示例

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

照片

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

检索照片列表。

参数

示例

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

Unsplash\Photo::find($id)

检索特定照片。

参数

示例

Unsplash\Photo::find($id);

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

代表用户发布照片。

注意:您需要write_photos权限范围。您需要首先实例化照片对象。

参数

示例

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

Unsplash\Photo::photographer()

检索照片的摄影师。

注意:您需要首先实例化一个照片对象。

参数

N/A

示例

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

Unsplash\Photo::random([featured => $value, username => $value, query => $value, w => $value, h => $value])

从指定过滤器中检索随机照片。有关过滤器的更多信息,请参阅官方文档

注意:需要一个数组作为参数。

参数

示例

// 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_photos权限范围。

参数

N/A

示例

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

Unsplash\Photo::unlike()

代表用户取消喜欢照片。

注意:您需要首先实例化一个照片对象。

注意:您需要like_photos权限范围。

参数

N/A

示例

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

Unsplash\Photo::statistics(string $resolution, int $quantity)

检索单个照片的总下载次数、查看次数和喜欢次数,以及在这些统计信息中特定时间段的详细历史分解(默认为30天)。

注意:您必须首先实例化照片对象。

参数

示例

$photo = Unsplash\Photo::find($id);
$photo->statistics('days', 7);

Unsplash\Photo::download()

触发照片的下载。这是遵循'触发下载' API指南所必需的。

注意:您必须首先实例化照片对象。

参数

示例

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

用户

Unsplash\User::find($username)

检索用户信息。

参数

示例

Unsplash\User::find($username)

Unsplash\User::portfolio($username)

检索用户作品集页面链接。

参数

示例

Unsplash\User::portfolio($username)

Unsplash\User::current()

检索用户的私人信息。

注意:您需要read_user权限范围。

参数

N/A

示例

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

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

检索用户照片。

注意:您需要首先实例化一个用户对象。

参数

示例

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

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

检索用户集合。

注意:您需要首先实例化一个用户对象。 注意:您需要read_collections权限范围才能检索用户的私人集合。

参数

示例

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

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

检索用户集合。

注意:您需要首先实例化一个用户对象。

参数

示例

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

Unsplash\User::update([$key => value])

更新当前用户字段。可以在数组中传递多个字段。

注意:您需要首先实例化一个用户对象。

注意:您需要write_user权限范围。

参数

$user = Unsplash\User::current();
$user->update(['first_name' => 'Elliot', 'last_name' => 'Alderson']);

Unsplash\User::statistics(string $resolution, int $quantity)

检索用户的总下载次数、查看次数和喜欢次数,以及在这些统计信息中特定时间段的详细历史分解(默认为30天)。

注意:您必须首先实例化User对象

参数

示例

$user = Unsplash\User::find($id);
$user->statistics('days', 7);

贡献

欢迎在GitHub上提交bug报告和pull请求:https://github.com/unsplash/unsplash-php。该项目旨在成为一个安全、友好的协作空间,并期望贡献者遵守贡献者公约的行为准则。