使用foreach

时间:2017-09-04 13:52:03

标签: php json wordpress instagram instagram-api

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

我遇到了问题。

我收到此错误:

  

E_WARNING:type 2 - 为foreach()提供的参数无效 - at   第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

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

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

修改 谢谢,大家,在评论中弄清楚了。

2 个答案:

答案 0 :(得分:0)

我不确切知道instagram API的层次结构,但我建议您尝试:

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

答案 1 :(得分:0)

您的$standardres['data']项目中包含图片,因此您必须在$standardres['data']循环中使用foreach,然后从项目数据中解析图片网址。

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