📅  最后修改于: 2023-12-03 15:05:46.027000             🧑  作者: Mango
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.
To start using the Unsplash PHP API, you will need to install it via Composer:
composer require unsplash/unsplash
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>']);
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'] . '">';
}
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'];
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.