Get all photos from Instagram which have a specific hashtag with PHP

To get all photos from Instagram that have a specific hashtag with PHP, you can use the Instagram API.

To use the Instagram API, you'll need to:

  1. Register as a developer on the Instagram Developer Platform
  2. Create an app and get an app ID and app secret
  3. Authenticate the app and get an access token
  4. Use the access token to make API requests to get the photos with the hashtag you specified

Watch a course Learn object oriented PHP

Here's some sample code that demonstrates how to use the Instagram API to get photos with a specific hashtag:

<?php

// Replace YOUR_APP_ID and YOUR_APP_SECRET with your actual app ID and app secret
$app_id = 'YOUR_APP_ID';
$app_secret = 'YOUR_APP_SECRET';

// Replace YOUR_HASHTAG with the hashtag you want to search for
$hashtag = 'YOUR_HASHTAG';

// Get an access token by making a POST request to the Instagram API
$access_token_url = 'https://api.instagram.com/oauth/access_token';
$access_token_data = [
  'client_id' => $app_id,
  'client_secret' => $app_secret,
  'grant_type' => 'client_credentials',
];

$curl = curl_init($access_token_url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $access_token_data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);

$result = json_decode($result);
$access_token = $result->access_token;

// Use the access token to make an API request to get the photos with the specified hashtag
$hashtag_url = 'https://api.instagram.com/v1/tags/' . $hashtag . '/media/recent';
$hashtag_data = [
  'access_token' => $access_token,
];

$curl = curl_init($hashtag_url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $hashtag_data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);

$result = json_decode($result);

// The $result variable now contains the data for the photos with the specified hashtag
// You can access the data for each photo like this:
foreach ($result->data as $photo) {
  $photo_url = $photo->images->standard_resolution->url;
  // Do something with the photo URL, like print it or save it to a database
  print $photo_url . "\n";
}

I hope this helps! Let me know if you have any questions.