Instagram API Technique to Retrieve and Save Data to a JSON File at Specified Time Intervals [PHP]
Displaying Instagram posts on your website is a great way to strengthen your online presence and increase engagement with visitors.
Instagram is well known for its visual content, and many websites utilize Instagram feeds.
In this article, we will introduce an easy way to display Instagram data on your website using the Instagram Graph API.
What You Need to Prepare
- Access permission to the Instagram API
- A web server that runs PHP
Before using the Instagram Graph API, you need to register an app with Facebook for Developers and obtain Instagram access permissions. Please refer to the official Facebook for Developers documentation for detailed steps.
Benefits of Displaying Instagram Posts on Your Website
By integrating Instagram content into your website, you can increase opportunities to convert social media followers into website visitors. It also enriches the visual content of your website and improves the overall user experience.
Important Points
When using the Instagram API, you need to pay attention to the request limit. Excessive requests may reach the API usage limit. Therefore, it is important to properly manage how frequently you retrieve data.
Specific PHP Implementation
The following PHP script is triggered each time a visitor views the site. It will retrieve JSON data from the Instagram Graph API every 30 minutes per day and save it on the web server as insta.json.
This script checks whether 30 minutes have passed since the last data retrieval, and if so, it fetches the data again. This can be achieved by recording the last retrieval time in a temporary file or database.
In the following example, a method is used where the last execution time is recorded in a file.
Replace YOUR_INSTAGRAM_GRAPH_API_URL_HERE with your actual Graph API URL. This should be the full request URL including the access token.
<?php
// Path to the file that records the last time the data was retrieved
$timestampFile = 'last_update_timestamp.txt';
// The JSON data output file
$jsonFile = 'insta.json';
// Instagram Graph API URL
$graphApiUrl = 'YOUR_INSTAGRAM_GRAPH_API_URL_HERE';
// Load the last time the data was retrieved
$lastUpdateTime = @file_get_contents($timestampFile);
$currentTime = time();
// Check if 30 minutes (1800 seconds) have passed since the last update
if ($lastUpdateTime === false || $currentTime - $lastUpdateTime > 1800) {
// Initialize cURL session
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $graphApiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
// Retrieve the response from the API
$response = curl_exec($ch);
curl_close($ch);
// Ensure the response is not false
if ($response !== false) {
// Save JSON data to a file
file_put_contents($jsonFile, $response);
// Update the last update timestamp
file_put_contents($timestampFile, $currentTime);
} else {
// Error handling
echo "Failed to retrieve data from the API.";
}
}
?>
Place this script in an appropriate location on your site so that it runs each time a visitor loads the page.
However, since this method depends on how frequently the site is visited, it may not work as expected if visitor patterns are unpredictable.
It may also affect page loading speed, so you should minimize performance impact by reducing execution time or implementing asynchronous processing.
Please make sure to comply with Instagram’s terms of use and API rate limits.
Finally
Adding dynamic content to your website using the Instagram API can be implemented easily, even for those with limited technical knowledge. We hope this article helps as a first step toward integrating Instagram data into your website. Give it a try!
※If you reuse this content, please do so at your own responsibility.
