📜  unsplash php api - PHP (1)

📅  最后修改于: 2023-12-03 15:05:46.027000             🧑  作者: Mango

Unsplash PHP API - PHP

Unsplash API is a free-to-use platform for finding high-quality photos and images. The Unsplash PHP API is a simple wrapper around the Unsplash API that makes it easy to work with the Unsplash API in PHP.

Features
  • Easy to use: The Unsplash PHP API is easy to use and can be integrated into any PHP project.
  • Fast: The API is designed to be fast and efficient, making it easy to work with large datasets of images.
  • Comprehensive: The API provides access to a comprehensive set of data about photos, including metadata such as keywords and tags.
Getting Started
Installation

To start using the Unsplash PHP API, you will need to install it via Composer:

composer require unsplash/unsplash
Authentication

Before you can start using the Unsplash PHP API, you need to create an account on the Unsplash website and obtain an access key. Once you have obtained your access key, you can authenticate your requests by passing your access key as a header:

$unsplash = new \Unsplash\Unsplash(['accessKey' => '<YOUR_ACCESS_KEY>']);
Search for photos

The Unsplash PHP API provides an easy way to search for photos based on keywords. To search for photos, simply call the search method and pass in the search term as a parameter:

$searchTerm = 'mountains';
$results = $unsplash->search($searchTerm)->getResults();

You can then loop through the results and display the images:

foreach ($results as $result) {
	echo '<img src="' . $result->getUrls()['regular'] . '">';
}
Retrieve a specific photo

You can retrieve a specific photo by ID using the photo method:

$photo = $unsplash->photo('<PHOTO_ID>');

You can then access the photo's metadata:

echo $photo['description'];
Conclusion

The Unsplash PHP API is a simple and efficient way to work with the Unsplash API in PHP. With its easy-to-use methods and comprehensive data, it is ideal for integrating high-quality images into your PHP projects.