You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PHP网站嵌入Instagram最新动态而非指定单条帖子?

解决PHP网站嵌入Instagram最新动态的两个问题

一、自动获取并展示最新帖子

Instagram官方提供基本展示API可实现获取公开账号的最新内容,具体操作步骤如下:

  1. 注册Meta开发者账号,创建应用并申请「基本展示API」权限
  2. 获取目标Instagram账号的user_id和长期访问令牌(Long-Lived Access Token)
  3. 在PHP中调用API接口获取并渲染最新帖子:
    <?php
    $accessToken = '你的长期访问令牌';
    $userId = '目标账号的user_id';
    // 调用API获取帖子数据
    $apiUrl = "https://graph.instagram.com/{$userId}/media?fields=id,caption,media_url,permalink&access_token={$accessToken}";
    
    // 发起请求(若服务器禁用file_get_contents,可替换为cURL)
    $response = file_get_contents($apiUrl);
    $posts = json_decode($response, true);
    
    // 渲染帖子
    if (!empty($posts['data'])) {
        foreach ($posts['data'] as $post) {
            echo "<a href='{$post['permalink']}' target='_blank'>";
            echo "<img src='{$post['media_url']}' alt='{$post['caption'] ?? 'Instagram帖子'}'>";
            echo "</a>";
            if (!empty($post['caption'])) {
                echo "<p>{$post['caption']}</p>";
            }
        }
    }
    ?>
    

二、修复账号嵌入仅显示图片的问题

你遇到的账号嵌入仅显示图片、无账号名称的情况,大多是因为嵌入代码不完整或缺失官方依赖脚本:

  1. 确保在页面<body>末尾引入Instagram官方嵌入脚本:
    <script async src="https://www.instagram.com/embed.js"></script>
    
  2. 使用完整的账号嵌入代码结构(可直接从Instagram官网生成:进入目标账号主页→右上角三点→嵌入,复制完整代码):
    <blockquote class="instagram-media" data-instgrm-permalink="https://www.instagram.com/你的账号名/" data-instgrm-version="14">
        <div style="padding:8px;">
            <a href="https://www.instagram.com/你的账号名/" target="_blank">@你的账号名</a>
        </div>
    </blockquote>
    

如果需要结合自动获取功能展示账号信息,可在PHP中额外调用API获取账号详情:

// 获取账号基本信息
$userInfoUrl = "https://graph.instagram.com/{$userId}?fields=username,profile_picture_url&access_token={$accessToken}";
$userResponse = file_get_contents($userInfoUrl);
$userInfo = json_decode($userResponse, true);

// 渲染账号信息
echo "<div class='instagram-profile'>";
echo "<img src='{$userInfo['profile_picture_url']}' alt='{$userInfo['username']}'>";
echo "<h3>@{$userInfo['username']}</h3>";
echo "</div>";

内容的提问来源于stack exchange,提问作者GMS

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 17:25:24