PHP: Find and download photos by keyword from Unsplash API

Updated: February 14, 2024 By: Guest Contributor Post a comment

Introduction

In this tutorial, we’ll dive into how to use the Unsplash API with PHP to search for photos by keyword and download them. Unsplash provides high-quality free images, shared by a community of photographers. Their API allows developers to access these images and incorporate them into their projects. This is particularly useful for applications, websites, or even scripts that need dynamic or specific imagery based on user input or other criteria.

Prerequisites

  • Basic knowledge of PHP
  • A development environment capable of running PHP (version 7.0 or greater)
  • Composer, for dependency management
  • An Unsplash account to access the API credentials (You can sign up for free)

Step 1: Setting Up Your Unsplash API Access

First, you need to register your application with Unsplash to get your API access keys. Follow these steps:

  1. Go to Unsplash Developers section and sign in or create an account.
  2. Click on “New Application”, agree to the terms, and fill in the application details.
  3. Once your application is approved, you’ll get an Access Key and a Secret Key. These will be necessary for authentication when making API requests.

Step 2: Installing GuzzleHttp

We’ll use GuzzleHttp, a PHP HTTP client, to make requests to the Unsplash API. You can install Guzzle through Composer by running the following command:

composer require guzzlehttp/guzzle

This command will download GuzzleHttp and its dependencies into your project folder.

Step 3: Making Your First API Request

To begin working with the Unsplash API, we need to construct a client using GuzzleHttp. This client will manage our requests to Unsplash. Here’s how to set it up:

$client = new \GuzzleHttp\Client();
$response = $client->request('GET', 'https://api.unsplash.com/search/photos', [
'query' => [
'query' => 'keyword',
'client_id' => 'YOUR_ACCESS_KEY'
]
]);

$data = json_decode($response->getBody(), true);

Replace 'keyword' with the term you’re interested in searching and 'YOUR_ACCESS_KEY' with the actual Access Key provided by Unsplash. This request searches for photos related to the specified keyword and returns a JSON object containing the results.

Step 4: Processing Results and Downloading Photos

Once you have the search results, you can loop through the data to access individual photos. Each photo object contains various details, including URLs to different image sizes. Here’s how you can access these details and download the medium size image:

foreach ($data['results'] as $photo) {
$photoUrl = $photo['urls']['small'];
$img = file_get_contents($photoUrl);
file_put_contents('/path/to/save/image.jpg', $img);
}

Make sure to replace '/path/to/save/image.jpg' with the actual path where you want to save the image. This code snippet demonstrates how to fetch each photo’s small-size URL, download the image, and save it to your server.

Handling API Rate Limits

It’s important to note that Unsplash imposes rate limits to protect its service. Ensure that your application respects these limits. You can check the remaining requests allowed by examining the headers of the API response:

$remainingRequests = $response->getHeader('X-Ratelimit-Remaining');

Consider implementing caching or storing the photos locally to minimize the number of requests.

Wrap-up

In this tutorial, we’ve covered how to use PHP to interact with the Unsplash API. We’ve learned how to set up your API access, make requests using GuzzleHttp, process the results, and download photos. Whether you’re building a website, an application, or just need a script to automate downloading images, these skills will help you leverage the power of the Unsplash API in your PHP projects.