使用 foreach 浏览 Instagram API 结果图像数组的正确方法

Right way of browsing Instagram API result array of images with foreach

我最近一直在为 wordpress 构建一个插件,它基本上使用 instagram API 获取图像 URL,然后将其放入短代码中。

我遇到了一个问题。

我收到这个错误:

E_WARNING : type 2 -- Invalid argument supplied for foreach() -- at line 22

而且我不知道我做错了什么。

我的 foreach 代码:

//define Access token
$accesst= "ACCESS_TOKEN_GOES_HERE";
//userid
$userid=USERID_GOES_HERE;
//image count to get
$count=20;
//get api contents
$content = file_get_contents('https://api.instagram.com/v1/users/self/media/recent/?access_token='.$accesst.'&count='.$count);
//converting JSON to object
$standardres = json_decode($content, true);
//array method
foreach($standardres['data'][0]['images']['standard_resolution']['url'] as $photo)
{
    print $photo['url'][0];
    echo "<br>";
}

我的 JSON var_dump 得到了这个:

https://pastebin.com/3RaL6EUA

访问代码当然在发布之前被删除了。

有人知道我做错了什么吗?

编辑: 谢谢大家,在评论里弄明白了

我不太清楚 instagram 的层次结构 API,但我建议你试试 :

foreach($standardres['data']['images'] as $photo) {
    print_r($photo); // to see what the array contains!
    echo $photo['standard_resolution']['url'];
    echo "<br>";
}

您的 $standardres['data'] 的项目有图像,因此您必须在 foreach 循环中使用 $standardres['data'],然后从项目数据中解析图像 url。

foreach($standardres['data'] as $item) {
    print $item['images']['standard_resolution']['url'];
    echo "<br>";
}