I've been recently building a plugin for wordpress which basically uses the instagram API to get an image URL and then place it in a short code.
And I've come to a problem.
I get this error:
E_WARNING : type 2 -- Invalid argument supplied for foreach() -- at line 22
and I have no idea what am I doing wrong.
My code for the 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>";
}
My JSON var_dump
got me this:
The access codes, were of course deleted before posting this.
Does anyone have a clue what I'm doing wrong?
EDIT: Thanks, everyone, got it figured out in the comments.
Your $standardres['data']
have items which have images, so you must use $standardres['data']
in the foreach
loop and then parse the image url from the item data.
foreach($standardres['data'] as $item) {
print $item['images']['standard_resolution']['url'];
echo "<br>";
}