简体   繁体   English

如何使用 PHP 从我自己的 Instagram 获取图像?

[英]How to get images from my own Instagram with PHP?

can somebody tell me please how can I get images from my own Instagram via PHP?有人可以告诉我如何通过 PHP 从我自己的 Instagram 获取图像吗? I have login name, password, app id, app secret.我有登录名、密码、应用程序 ID、应用程序机密。 IG changed the api so all the documentation including how to get access token does not work. IG 更改了 api,因此包括如何获取访问令牌在内的所有文档都不起作用。 I have this peace of code:我有这样的代码和平:

$fb = new \Facebook\Facebook([
    'app_id' => self::APP_ID_IG,
    'app_secret' => self::APP_SECRET_IG,
    'default_graph_version' => 'v2.10',
]);

// This return string which creates access token as "app_id value|app_secret value"
$token = $fb->getApp()->getAccessToken();

// This line throws me an error: 
// Error validating application. Cannot get application info due to a system error.
$response = $fb->get('https://graph.instagram.com/me/media?fields=media_url,media_type', $token->getValue());

I am lost in it.我迷失在其中。 Thanks for help.感谢帮助。

You should take a look at the Instagram's Media API你应该看看Instagram 的 Media API

This endpoint should work to query the user's media: GET /{ig-user-id}/media giving you a list of media ids:此端点应该用于查询用户的媒体: GET /{ig-user-id}/media为您提供媒体 ID 列表:

{
  "data": [
    {
      "id": "17895695668004550"
    },
    {
      "id": "17899305451014820"
    },
    {
      "id": "17896450804038745"
    },
    {
      "id": "17881042411086627"
    },
    {
      "id": "17869102915168123"
    }
  ]
}

After that, I think you sould use the graph API to get the image URL of each media id.之后,我认为您应该使用图形 API 来获取每个媒体 ID 的图像 URL。

I created a vuejs adaption a while back, maybe this code structure snippet helps you since I don't have time right now to translate it into php , it is written in javascript but the API requests remain the same (just use curl instead of axios ): https://gitlab.com/-/snippets/1957175不久前我创建了一个 vuejs 适配,也许这个代码结构片段可以帮助你,因为我现在没有时间将它翻译成php ,它是用javascript编写的,但 API 请求保持不变(只需使用curl而不是axios ): https://gitlab.com/-/snippets/1957175

General steps:一般步骤:

  1. Get your authorization code either via a Login Window or via a facebook app (described below)通过登录窗口或 Facebook 应用程序获取您的授权码(如下所述)

  2. Transform it into a shortterm access token

curl --request POST \
  --url 'https://api.instagram.com/oauth/access_token?grant_type=authorization_code&client_id=[CLIENT_ID_HERE]&client_secret=[CLIENT_SECRET_HERE]&redirect_uri=[REDIRECT_URI_HERE]&code=[AUTHORIZATION_CODE_HERE]' \
  --header 'Content-Type: multipart/form-data' \
  --form client_id=[CLIENT_ID_HERE] \
  --form client_secret=[CLIENT_SECRET_HERE] \
  --form code=[AUTHORIZATION_CODE_HERE] \
  --form grant_type=authorization_code \
  --form redirect_uri=https://[REDIRECT_URI_HERE]/
  1. Transform that into a longterm access token 将其转换为长期访问令牌
GET https://graph.instagram.com/access_token?grant_type=ig_exchange_token&access_token=[SHORTTERM_ACCESS_TOKEN_HERE]&client_secret=[CLIENT_SECRET_HERE]
  1. Query the Media Graph endpoint with the URL from my snippet using the longterm access token as authorization.使用长期访问令牌作为授权,使用我的代码段中的 URL 查询媒体图端点。
  2. Refresh that longterm access token every 60 days ( see my answer on the topic on Github here ).每 60 天刷新一次长期访问令牌( 请参阅我对 Github 主题的回答)。 You could totally automate that step via for example via CRON.例如,您可以通过 CRON 完全自动化该步骤。
GET https://graph.instagram.com/refresh_access_token?grant_type=ig_refresh_token&access_token=[OLD_ACCESS_TOKEN_HERE]

For more info about which field to add to the query check the official facebook documentation有关将哪个字段添加到查询中的更多信息,请查看官方 facebook 文档


Getting an authorization code via a facebook app:通过 Facebook 应用获取授权码:

  1. Log into your facebook developer account登录您的Facebook 开发者帐户
  2. Create a new Application创建一个新的应用程序
  3. Under products, add Instagram Basic Display and fill out all necessary fields在产品下,添加Instagram Basic Display并填写所有必填字段

Instagram 基本显示

  1. Within the section Basic Display of the Instagram Basic Display product you will find your App ID and SecretInstagram Basic Display产品的Basic Display部分中,您将找到您的App IDSecret 应用程序 ID 和机密

  2. Below that you can enter your redirect_uri您可以在下方输入您的redirect_uri

重定向uri

  1. Finally below that is the User Token Generator which you can use to最后是用户令牌生成器,您可以使用它

[...]quickly generate long-lived Instagram User Access Tokens for any of your public Instagram accounts. [...] 为您的任何公共 Instagram 帐户快速生成长期存在的 Instagram 用户访问令牌。 This is useful if you [...] don't want to bother with implementing the Authorization Window [...] See Instagram Basic Display API Docs如果您 [...] 不想为实现授权窗口而烦恼,这将很有用 [...] 请参阅 Instagram 基本显示 API 文档

  1. Continue with 3. of General Steps继续3.一般步骤

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM