You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何使用PHP调用Instagram Graph API获取公开帖子信息及入门帮助

Hey there! I’ve made the switch from the old Instagram API to Graph API too, so I know exactly where you’re coming from. Let me walk you through a straightforward, step-by-step guide to get those basic public details (username, like counts, post dates, image URLs, captions) using Instagram Graph API:

Instagram Graph API 入门:获取账号基础公开信息

1. First, Get Your Setup Right

Before diving into calls, you’ll need a few things sorted:

  • A Facebook Developer Account: Graph API is tied to the Facebook platform, so head over there to create an app and add the Instagram Graph API product to it.
  • An Instagram Business or Creator Account: Unlike the old API, you can’t pull data from regular personal accounts via Graph API. If you’re working with your own account, switch it to Business/Creator first (it’s free and easy to do in Instagram settings).
  • A Valid access_token: For your own account, the easiest way is to get a user-level token by authorizing your app with your Facebook page (since Business/Creator accounts link to a Facebook page). You’ll need the pages_show_list and instagram_basic permissions during authorization.

2. Core API Calls to Fetch Your Data

Let’s break this down into two parts: getting account info, then pulling post details.

Step 1: Grab the Instagram Business Account ID

First, you need the unique ID of your Instagram Business/Creator account. You can get this by querying your linked Facebook Page ID:

GET /{your-facebook-page-id}?fields=instagram_business_account

This will return the instagram_business_account object with its id—jot that down, you’ll use it for all subsequent calls.

Step 2: Get Basic Account Info (Username)

Use the ID from the last step to fetch your account’s username:

GET /{instagram-business-account-id}?fields=username,name

The response will include username (the handle people use to find you) and name (your display name on Instagram).

Step 3: Fetch Post Details (Likes, Date, Image URL, Caption)

To get all your posts along with the data you need, use this endpoint:

GET /{instagram-business-account-id}/media?fields=id,caption,timestamp,like_count,media_url

Each post in the response will have exactly what you’re looking for:

  • caption: The post’s description text
  • timestamp: The date/time the post was published (in ISO 8601 format, e.g., 2024-05-20T14:22:00+0000)
  • like_count: Total number of likes on the post
  • media_url: Direct URL to the post’s image (or video)
  • id: The post’s unique ID, useful if you want to fetch a single post later

If you need details for a specific post, just use its ID:

GET /{specific-post-id}?fields=id,caption,timestamp,like_count,media_url

3. Key Things to Keep in Mind

  • Permissions Check: Make sure your access_token has the instagram_basic and pages_show_list permissions. You can verify your token with this call:
    GET /debug_token?input_token={your-access-token}&access_token={your-app-id}|{your-app-secret}
    
  • Rate Limits: Graph API has rate limits to prevent abuse. If you’re testing, keep calls spaced out—you’ll get an error if you hit the limit.
  • Personal Account Restriction: I can’t stress this enough—regular personal Instagram accounts won’t work with Graph API. You must switch to Business or Creator.

4. Quick Troubleshooting

I’m not getting any data back. What’s wrong?

  • Double-check that your Instagram account is linked to a Facebook Page, and that you’re using the correct Page ID in your initial call.
  • Verify your access_token is valid and has the required permissions.
  • Ensure you’re using the Instagram Business Account ID (not the Facebook Page ID or regular Instagram user ID) in your media calls.

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

火山引擎 最新活动