如何使用 php 获取最新的 Instagram 帖子

How to get latest instagram posts using php

我想从我的 Instagram 帐户获取最新帖子,我正在使用这个 api url:https://api.instagram.com/v1/users/[USER_ID]/media/recent?access_token=[ACCESS_TOKEN]

当我在浏览器中粘贴此 url 时,我可以看到字符串 JSON 但我找不到对其进行解码和使用对象属性的方法。我正在使用 PHP 和 cURL 来获取和解码 JSON。它总是 return NULL。我的 cURL 函数是这样的:

function fetchData($url){
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);     
  curl_setopt($ch, CURLOPT_URL, $url);
  $result = curl_exec($ch);
  curl_close($ch);
  return $result;
}

最后:

$url = https://api.instagram.com/v1/users/[USER_ID]/media/recent?access_token=[ACCESS_TOKEN]
$result = fetchData($url);
var_dump(json_decode($result));

我在这里做错了什么? 我还测试了 file_get_contentscURL 但它给了我这个错误:

No connection could be made because the target machine actively refused it

此端点的问题:

https://api.instagram.com/v1/users/[USER_ID]/media/recent?access_token=[ACCESS_TOKEN]

无法使用您自己的访问令牌获取 public 媒体。只能使用生成的令牌获取自媒体。所以正确的端点应该是

https://api.instagram.com/v1/users/self/media/recent?access_token=[ACCESS_TOKEN]

这适用于服务器和客户端,但因为必须保护 instagram 访问令牌,所以我正在使用服务器端脚本来获取最新帖子。