Front-End & Daily

How to Embed Posts Using Instagram API and PHP with Screenshots [2026 Edition]

How to Embed Posts Using Instagram API and PHP with Screenshots [2026 Edition]

We will introduce how to retrieve Instagram posts using Instagram Graph API and PHP and embed them into a web page.

The Instagram API often changes its specifications, making it difficult to get started, but I've compiled the method as of 2026 from scratch.

If the specifications change in the future, I'd appreciate it if you could let me know in the comments.

Table of Contents

Switch Instagram Account to Professional Account

First, switch the Instagram account from which you want to retrieve posts to a "Professional Account".

Rest assured, switching to a Professional Account does not incur any fees.
Please note that if you switch a private account to a Professional Account, it will become public.

From Instagram settings, Account Type and Tools, select "Switch to Professional Account".

Instagram Account Type and Tools Switch to Professional Account

Select a type.
I believe either is fine, but choose the one that applies to you. (Can be changed later)

Instagram Professional Account Type Creator Business

Select a category. (Can be changed later)

Instagram Professional Account Select Category

This completes the switch to a Professional Account.

Create a Facebook Page and Link it to Your Instagram Account

To use the Instagram API, you need to create a Facebook "Page", which acts as an interface for making API calls.

From Facebook, open "Pages".

Facebook Pages

"Create New Page"

Facebook Create New Page

Select "Public Page" and click Next

Facebook Create New Page Public Page

Enter "Page name" and "Category" and click "Create Facebook Page".

Facebook Create New Page

The Facebook Page has been created.
Next, we will link it to your Instagram account.

From your Facebook profile in the top right, with the newly created page selected, click "Settings & Privacy > Settings".

Facebook Page Settings & Privacy

From "Linked Accounts", click "Instagram", then log in with the Instagram account you just switched to a Professional Account and link it.

Facebook Page Linked Accounts Instagram

This completes linking your Facebook Page and Instagram account.

Create an App in Meta for Developers and Obtain a Token

We will create an app in Meta for Developers and obtain the "Instagram Account ID" and "Access Token".

Log in to Meta for Developers. (Registration may be required for first-time users)

From My Apps, click "Create App".

Meta for Developers Create App

Enter "App name" and "Email address" and click Next.

Meta for Developers App Details App Name App Contact Email Address

Add a use case.
This is like setting what role your app will have.

This time, select "Other > Other".

Meta for Developers Use Case Other Other This option is going away soon The app will be created with an older version.

Although it says "older version", after trying various options, I found that the older version provides more information, so I adopted it.

Select "Business" for the app type.

Meta for Developers App Type Business

Confirm the app name and email address, then click "Create App".

Meta for Developers Create App

The app has been created.

From "Add Product", click "Set up" for Instagram to add Instagram Graph API.

Meta for Developers Add Product Instagram

Add your Instagram account as an Instagram Tester for this app.

Open "App Roles" in the side menu, click "Add People" in the top right, select "Instagram Testers", enter your Instagram ID, and click Add.

Meta for Developers Add People to App Instagram Testers

Since it will be "Pending", open Instagram from the Apps and Websites link.

Meta for Developers App Roles Pending

In Instagram, from the "Tester Invites" tab, click "Accept" for the app you just requested.

Instagram Apps and Websites Tester Invites Accept

Next, we will obtain the "Instagram Account ID" and "Access Token".

The access token is obtained in

  1. First, obtain a token valid for 1 hour.
  2. Then, extend it to obtain a token valid for 3 months.

a two-step process.

Since the maximum validity is 3 months, we will eventually implement a process in PHP or similar to refresh the access token every 3 months.

Open "Tools > Graph API Explorer" from the menu.

Meta for Developers Graph API Explorer

Confirm that "Meta App" is the app you are currently editing.
For "Permissions", add "instagram_basic" if you only want to retrieve your own posts.
As mentioned later, if you want to retrieve other people's posts, also add "instagram_basic" and "instagram_manage_insights".
Click "Generate Access Token" and copy the access token displayed above it.

Meta for Developers Graph API Explorer Generate Access Token

The access token obtained here will be valid for 1 hour.
We will now extend its validity.

Open "Tools > Access Token Debugger" from the menu.

Meta for Developers Access Token Debugger

Paste the access token you just copied at the top and click "Debug".
Confirm that the expiration is "within approximately 1 hour".
The string of numbers under "instagram_basic" is your "Instagram Account ID", so make a note of it.
Click "Extend Access Token". Copy the output access token (valid for 3 months).

Meta for Developers Access Token Debugger Extend Access Token

With this, you have obtained the "Instagram Account ID" and "Access Token (valid for 3 months)".

Finally, let's enter the basic settings for the app from "App Settings > Basic".

I believe it's sufficient to at least fill in "Display Name", "App Domains", "App Icon", and "Contact Email".

Meta for Developers App Settings Basic

Sample Code for Retrieving Posts with PHP

Here's a sample PHP code for making API calls using the obtained "Instagram Account ID" and "Access Token".

We will introduce the code in the following three-part structure +α.

Most Basic Code

First, here's the most basic code. However, this code has several issues, which will be explained next.
Please replace 'Access Token' and 'Instagram Account ID' with your own.

// =============================================
// Settings
// =============================================
$access_token      = 'ここにアクセストークンを入力'; //Enter your access token here
$instagram_user_id = 'ここにInstagramアカウントIDを入力'; //Enter your Instagram account ID here

// Fields to retrieve
$fields = 'id,caption,media_type,timestamp,permalink,thumbnail_url,media_url';

// =============================================
// API Request
// =============================================
$url = "https://graph.facebook.com/v19.0/{$instagram_user_id}/media?fields={$fields}&access_token={$access_token}";

$response = file_get_contents($url);

if ($response === false) {
	die('API request failed.');
}

$data = json_decode($response, true);

// Error check
if (isset($data['error'])) {
	die('API error: ' . $data['error']['message']);
}

// =============================================
// Display results
// =============================================
$posts = $data['data'];

echo "Number of posts: " . count($posts) . " items\n";

foreach ($posts as $post) {
	echo "ID        : " . ($post['id']           ?? '') . "\n";
	echo "Type      : " . ($post['media_type']   ?? '') . "\n";
	echo "Date/Time : " . ($post['timestamp']    ?? '') . "\n";
	echo "Caption   : " . ($post['caption']    ?? '(none)') . "\n";
	echo "URL       : " . ($post['permalink']    ?? '') . "\n";
	echo "Image URL : " . ($post['media_url']    ?? '') . "\n";
}

With the code above, the API is called every time the page is opened, quickly reaching the rate limit.
Therefore, it's necessary to implement measures such as caching the data in a JSON file once a day and loading posts from the cache on that day.

Code to Cache Data Once a Day

Calling the API every time the page is opened quickly leads to rate limits. Therefore, it's necessary to implement a strategy such as saving retrieved posts as a cache once a day and loading them from the cache on that day.

Below is a sample code that caches post data once a day.
A file named "instagram.json" will be generated in the directory where this PHP file is located, and the cache will be recorded there.
It checks the timestamp of "instagram.json", and if more than 24 hours have passed, it makes a new API call to re-cache the data.

// =============================================
// Settings
// =============================================
$access_token      = 'ここにアクセストークンを入力'; //Enter your access token here
$instagram_user_id = 'ここにInstagramアカウントIDを入力'; //Enter your Instagram account ID here

// Fields to retrieve
$fields = 'id,caption,media_type,timestamp,permalink,thumbnail_url,media_url';

// =============================================
// Cache processing
// =============================================
$cache_file = __DIR__ . '/instagram.json';

// Check if cache is valid (within 24 hours)
if (file_exists($cache_file) && time() - filemtime($cache_file) 

Code to Cache Data and Automatically Refresh Access Token

Since Instagram Graph API access tokens are valid for a maximum of 3 months, it's necessary to either manually refresh them periodically or implement a method for automatic refreshing.

To automatically refresh the access token, you need the "App ID" and "App Secret", which you should note down from "App Settings > Basic".

Meta for Developers App ID app secret

Below is a sample code that automatically refreshes the access token and caches posts once a day.
In the directory where this PHP file is located, "instagram.json" and "token.json" will be generated, handling post caching once a day and automatic token refreshing once every 30 days, respectively.

// =============================================
// Settings
// =============================================
$access_token      = '初回のみ、ここにアクセストークンを入力'; //Enter access token here for the first time only
$instagram_user_id = 'ここにInstagramアカウントIDを入力'; //Enter your Instagram account ID here
$app_id            = 'ここにアプリIDを入力'; //Enter your App ID here
$app_secret        = 'ここにapp secretを入力'; //Enter your app secret here

// Fields to retrieve
$fields = 'id,caption,media_type,timestamp,permalink,thumbnail_url,media_url';

// Cache file paths
$cache_file = __DIR__ . '/instagram.json';
$token_file = __DIR__ . '/token.json';

// =============================================
// Token auto-refresh process
// =============================================

// Load token file if it exists
if (file_exists($token_file)) {
	$token_data     = json_decode(file_get_contents($token_file), true);
	$access_token   = $token_data['access_token'];
	$token_saved_at = $token_data['saved_at'];
} else {
	$token_saved_at = 0;
}

// Refresh token if more than 30 days have passed
if (time() - $token_saved_at > 86400 * 30) {
	$refresh_url = "https://graph.facebook.com/v19.0/oauth/access_token?grant_type=fb_exchange_token&client_id={$app_id}&client_secret={$app_secret}&fb_exchange_token={$access_token}";

	$refresh_response = file_get_contents($refresh_url);

	if ($refresh_response !== false) {
		$refresh_data = json_decode($refresh_response, true);

		if (isset($refresh_data['access_token'])) {
			$access_token = $refresh_data['access_token'];

			// Save new token
			file_put_contents($token_file, json_encode([
				'access_token' => $access_token,
				'saved_at'     => time(),
			]));

			// Token refreshed, so force re-fetching post cache
			if (file_exists($cache_file)) {
				unlink($cache_file);
			}
		}
	}
}

// =============================================
// Cache processing
// =============================================

// Check if cache is valid (within 24 hours)
if (file_exists($cache_file) && time() - filemtime($cache_file) 

Code to Retrieve Posts from Clients (Other People)

You can embed Instagram posts from other people (such as clients) by using your own Instagram account and Meta app as a receiver.

Access token permissions will require not only "instagram_basic" but also "instagram_manage_insights". (As mentioned earlier)

Also, when embedding other people's posts, please proceed with discretion, such as obtaining their consent.

Here is the sample code.

// =============================================
// Settings
// =============================================
$access_token      = 'ここにアクセストークンを入力'; //Enter your access token here
$instagram_user_id = 'ここにInstagramアカウントIDを入力'; //Enter your Instagram account ID here
$target_user       = 'ここに埋め込みたいアカウントのIDを入力'; //Enter the ID of the account you want to embed here

// Fields to retrieve
$fields = 'business_discovery.username(' . $target_user . '){username,followers_count,media_count,media{id,caption,media_type,timestamp,permalink,thumbnail_url,media_url}}';

// =============================================
// API Request
// =============================================
$url = "https://graph.facebook.com/v19.0/{$instagram_user_id}?fields={$fields}&access_token={$access_token}";

$response = file_get_contents($url);

if ($response === false) {
	die('API request failed.');
}

$data = json_decode($response, true);

// Error check
if (isset($data['error'])) {
	die('API error: ' . $data['error']['message']);
}

// =============================================
// Display results
// =============================================
$discovery = $data['business_discovery'];
$posts     = $discovery['media']['data'];

echo "Account Name: " . ($discovery['username']        ?? '') . "\n";
echo "Followers   : " . ($discovery['followers_count'] ?? '') . "\n";
echo "Posts       : " . ($discovery['media_count']     ?? '') . " items\n\n";

foreach ($posts as $post) {
	echo "ID          : " . ($post['id']          ?? '') . "\n";
	echo "Type        : " . ($post['media_type']  ?? '') . "\n";
	echo "Date/Time   : " . ($post['timestamp']   ?? '') . "\n";
	echo "Caption     : " . ($post['caption']     ?? '(none)') . "\n";
	echo "URL         : " . ($post['permalink']   ?? '') . "\n";
	echo "Image URL   : " . ($post['media_url']   ?? '') . "\n";
}

This code omits the caching mechanism and automatic token refresh mechanism, so please combine them as needed.

That concludes the 2026 edition of the steps and sample code for embedding posts using Instagram Graph API and PHP.

Comments

After reviewing the content, we will publish it, omitting any personal information.

Enter your name and email address as well

Please enter if you would like a reply by email.

Any personal information provided will not be disclosed and will only be used for replies.

It will be sent directly. Please confirm and click 'Send'.

If this was helpful, we appreciate your support!
All support received will be used for child-rearing.

Author's Baby Registry (Amazon)

Send Support via OFUSE

Or support me by buying something through the buttons below
(You don't have to buy the linked product.)

Support me via Amazon

Support me via Rakuten

Support me via Yahoo!Shopping

PR

As an Amazon Associate, Ken earns from qualifying purchases.

Share

Share on Twitter Share on Facebook Share on LINE Share on Hatena Bookmark

Author's Baby Registry (Amazon)