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]](img/ogp.png)
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.
We also offer Instagram embedding services!
Depending on the requirements, we can start from around 7,000 yen.
We will create the app and program, so you only need to switch to a professional account as described in this article.
Even just a consultation is fine. Please feel free to contact us here.
Table of Contents
Switching your Instagram account to a Professional Account
First, switch the Instagram account from which you want to retrieve posts to a "Professional Account".
Don't worry, switching to a Professional Account does not incur any fees.
Please note that if you switch a private account (locked account) to a Professional Account, it will become public.
From Instagram settings, Account Type and Tools, select "Switch to Professional Account".
Select a type.
I think either is fine, but choose the one that applies to you. (Can be changed later)
Select a category. (Can be changed later)
This completes the switch to a Professional Account.
Creating a Facebook Page and linking it to your Instagram account
To use the Instagram API, you need to create a Facebook "Page", which acts as a gateway for making API calls.
From Facebook, open "Pages".
"Create New Page"
Select "Public Page" and click Next
Enter "Page name" and "Category" and click "Create Facebook 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".
From "Linked accounts", click "Instagram", then log in with the Instagram account you just switched to a Professional Account and link it.
This completes the linking of your Facebook Page and Instagram account.
Creating an app in Meta for Developers and obtaining 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. (You might need to register for the first time)
From My Apps, click "Create App".
Enter "App name" and "Email address" and click Next.
Add a use case.
This is like setting what role your app will have.
This time, select "Other > Other".
Although it says 'older version', after trying various options, I found that the older version provides more information, so I adopted it.
Select "Business" as the app type.
Confirm the app name and email address, then click "Create App".
The app has been created.
From "Add Product", click "Set up" for Instagram to add Instagram Graph API.
Add your Instagram account to this app's Instagram Testers.
Open 'App Roles' in the side menu, then from "Add Members" in the top right, select "Instagram Testers", enter your Instagram ID, and click Add.
Since it will be 'Pending', open Instagram from the Apps and Websites link.
In Instagram, from the "Tester Invites" tab, click "Accept" for the app you just requested.
Next, we will obtain the "Instagram Account ID" and "Access Token".
The access token is obtained in two steps:
- First, obtain one with a 1-hour validity period.
- Then, extend it to obtain one with a 3-month validity period.
Since the maximum validity period is 3 months, we will eventually add a process in PHP or similar to refresh the access token every 3 months.
Open "Tools > Graph API Explorer" from the menu.
Check if "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.
The access token obtained here has a 1-hour validity period.
We will now extend this validity period.
Open "Tools > Access Token Debugger" from the menu.
Paste the access token you just copied at the top and click "Debug".
Confirm that the expiration date is "Within about 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 (3-month validity).
With this, you have obtained your "Instagram Account ID" and "Access Token (3-month validity)".
Finally, let's enter the basic settings for the app from "App Settings > Basic".
I think it's sufficient to at least fill in 'Display Name', 'App Domains', 'App Icon', and 'Contact Email Address'.
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 +α.
- The most basic code
- Code to cache data once a day
- Code to cache data and automatically refresh the access token
- Code to retrieve posts from clients (other people)
The most basic code
First, here's the most basic code. However, this code has a few issues, which will be explained next.
Please replace 'Access Token' and 'Instagram Account ID' with your own.
// =============================================
// 設定
// =============================================
$access_token = 'ここにアクセストークンを入力'; //置き換え
$instagram_user_id = 'ここにInstagramアカウントIDを入力'; //置き換え
// 取得するフィールド
$fields = 'id,caption,media_type,timestamp,permalink,thumbnail_url,media_url';
// =============================================
// APIリクエスト
// =============================================
$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リクエストに失敗しました。');
}
$data = json_decode($response, true);
// エラーチェック
if (isset($data['error'])) {
die('APIエラー: ' . $data['error']['message']);
}
// =============================================
// 結果の表示
// =============================================
$posts = $data['data'];
echo "投稿数: " . count($posts) . "件\n";
foreach ($posts as $post) {
echo "ID : " . ($post['id'] ?? '') . "\n";
echo "種類 : " . ($post['media_type'] ?? '') . "\n";
echo "日時 : " . ($post['timestamp'] ?? '') . "\n";
echo "キャプション: " . ($post['caption'] ?? '(なし)') . "\n";
echo "URL : " . ($post['permalink'] ?? '') . "\n";
echo "画像URL : " . ($post['media_url'] ?? '') . "\n";
}
With the code above, the API is called every time the page is opened, which will quickly hit the rate limit.
Therefore, it's necessary to implement measures such as caching the posts in a JSON file once a day and loading them from the cache for the rest of the day.
Code to cache data once a day
Calling the API every time the page is opened will quickly hit the rate limit, so it's necessary to implement measures such as saving the retrieved posts as a cache once a day and loading them from the cache for the rest of the 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.
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.
// =============================================
// 設定
// =============================================
$access_token = 'ここにアクセストークンを入力'; //置き換え
$instagram_user_id = 'ここにInstagramアカウントIDを入力'; //置き換え
// 取得するフィールド
$fields = 'id,caption,media_type,timestamp,permalink,thumbnail_url,media_url';
// =============================================
// キャッシュ処理
// =============================================
$cache_file = __DIR__ . '/instagram.json';
// キャッシュが有効か確認 (24時間以内)
if (file_exists($cache_file) && time() - filemtime($cache_file) < 86400) {
$posts = json_decode(file_get_contents($cache_file), true);
} else {
// APIリクエスト
$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リクエストに失敗しました。');
}
$data = json_decode($response, true);
// エラーチェック
if (isset($data['error'])) {
die('APIエラー: ' . $data['error']['message']);
}
$posts = $data['data'];
// キャッシュ保存
if (!is_dir(dirname($cache_file))) {
mkdir(dirname($cache_file), 0755, true);
}
file_put_contents($cache_file, json_encode($posts));
}
// =============================================
// 結果の表示
// =============================================
echo "投稿数: " . count($posts) . "件\n\n";
foreach ($posts as $post) {
echo "ID : " . ($post['id'] ?? '') . "\n";
echo "種類 : " . ($post['media_type'] ?? '') . "\n";
echo "日時 : " . ($post['timestamp'] ?? '') . "\n";
echo "キャプション: " . ($post['caption'] ?? '(なし)') . "\n";
echo "URL : " . ($post['permalink'] ?? '') . "\n";
echo "画像URL : " . ($post['media_url'] ?? '') . "\n";
}
Code to cache data and automatically refresh the access token
Since Instagram Graph API access tokens can only be obtained for a maximum of 3 months, it's necessary to either manually refresh the access token regularly or implement a mechanism 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".
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 token auto-refreshing once every 30 days, respectively.
// =============================================
// 設定
// =============================================
$access_token = '初回のみ、ここにアクセストークンを入力'; //置き換え
$instagram_user_id = 'ここにInstagramアカウントIDを入力'; //置き換え
$app_id = 'ここにアプリIDを入力';
$app_secret = 'ここにapp secretを入力';
// 取得するフィールド
$fields = 'id,caption,media_type,timestamp,permalink,thumbnail_url,media_url';
// キャッシュファイルのパス
$cache_file = __DIR__ . '/instagram.json';
$token_file = __DIR__ . '/token.json';
// =============================================
// トークン自動更新処理
// =============================================
// トークンファイルが存在する場合は読み込む
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;
}
// 30日以上経過していたらトークンを更新する
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'];
// 新しいトークンを保存
file_put_contents($token_file, json_encode([
'access_token' => $access_token,
'saved_at' => time(),
]));
// トークンが更新されたので投稿キャッシュも再取得させる
if (file_exists($cache_file)) {
unlink($cache_file);
}
}
}
}
// =============================================
// キャッシュ処理
// =============================================
// キャッシュが有効か確認 (24時間以内)
if (file_exists($cache_file) && time() - filemtime($cache_file) < 86400) {
$posts = json_decode(file_get_contents($cache_file), true);
} else {
// APIリクエスト
$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リクエストに失敗しました。');
}
$data = json_decode($response, true);
// エラーチェック
if (isset($data['error'])) {
die('APIエラー: ' . $data['error']['message']);
}
$posts = $data['data'];
// キャッシュ保存
file_put_contents($cache_file, json_encode($posts));
}
// =============================================
// 結果の表示
// =============================================
echo "投稿数: " . count($posts) . "件\n\n";
foreach ($posts as $post) {
echo "ID : " . ($post['id'] ?? '') . "\n";
echo "種類 : " . ($post['media_type'] ?? '') . "\n";
echo "日時 : " . ($post['timestamp'] ?? '') . "\n";
echo "キャプション: " . ($post['caption'] ?? '(なし)') . "\n";
echo "URL : " . ($post['permalink'] ?? '') . "\n";
echo "画像URL : " . ($post['media_url'] ?? '') . "\n";
}
Code to retrieve posts from clients (other people)
You can embed Instagram posts from other people (clients, etc.) 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, do so with discretion, such as obtaining their consent.
Here is the sample code.
// =============================================
// 設定
// =============================================
$access_token = 'ここにアクセストークンを入力'; //置き換え
$instagram_user_id = 'ここにInstagramアカウントIDを入力'; //置き換え
$target_user = 'ここに埋め込みたいアカウントのIDを入力'; //置き換え
// 取得するフィールド
$fields = 'business_discovery.username(' . $target_user . '){username,followers_count,media_count,media{id,caption,media_type,timestamp,permalink,thumbnail_url,media_url}}';
// =============================================
// APIリクエスト
// =============================================
$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リクエストに失敗しました。');
}
$data = json_decode($response, true);
// エラーチェック
if (isset($data['error'])) {
die('APIエラー: ' . $data['error']['message']);
}
// =============================================
// 結果の表示
// =============================================
$discovery = $data['business_discovery'];
$posts = $discovery['media']['data'];
echo "アカウント名: " . ($discovery['username'] ?? '') . "\n";
echo "フォロワー数: " . ($discovery['followers_count'] ?? '') . "\n";
echo "投稿数 : " . ($discovery['media_count'] ?? '') . "件\n\n";
foreach ($posts as $post) {
echo "ID : " . ($post['id'] ?? '') . "\n";
echo "種類 : " . ($post['media_type'] ?? '') . "\n";
echo "日時 : " . ($post['timestamp'] ?? '') . "\n";
echo "キャプション: " . ($post['caption'] ?? '(なし)') . "\n";
echo "URL : " . ($post['permalink'] ?? '') . "\n";
echo "画像URL : " . ($post['media_url'] ?? '') . "\n";
}
This code omits the caching mechanism and automatic token refresh mechanism, so please use it in combination with those.
That concludes the steps and sample code for embedding posts using Instagram Graph API and PHP, 2026 Edition.




























If this was helpful, we appreciate your support!
All support received will be used for childcare.
Author's Baby Registry (Amazon)
Send support via OFUSE
Alternatively, support me by buying something through the buttons below
(You don't have to buy the specific product linked.)
Support me via Amazon
Support me via Rakuten
Support me via Yahoo!Shopping
PR