I’m using this structure for Instagram:
<?php
// Supply a user id and an access token
$userid = "--user--";
$accessToken = "--token--";
// Gets our data
function fetchData($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
// Pulls and parses data.
$result = fetchData("https://api.instagram.com/v1/users/--user--/media/recent/?access_token=--token--");
$result = json_decode($result);
?>
<?php foreach ($result->data as $post): ?>
<!-- Renders images. @Options (thumbnail,low_resoulution, high_resolution) -->
<a class="group" rel="group1" href="<?= $post->images->standard_resolution->url ?>"><img src="<?= $post->images->thumbnail->url ?>"></a>
<?php endforeach ?>
But this print:
Warning: Invalid argument supplied for foreach() in /home/mysite/index.php on line 40
40.line:
<?php foreach ($result->data as $post): ?>
Whats wrong in this structure?
This structure is http://www.blueprintinteractive.com/blog/how-instagram-api-fancybox-simplified
Ok, just to check – in your code, you have
$userid = "--user--"and$accessToken = "--token--", then in the URL, you have--user--and--token--again – you are replacing them with$useridand$accessToken? If not, the issue could simply be that Instagram is returning an error, which in turn caused your code to fail.If you have taken the above in to account and are still getting nothing, then print the value of
$result->databefore the loop (var_dump($result->data)) and see what your variable contains.If the above shows nothing, then try
var_dump($result)– It is possible that thejson_decode()has failed and will showfalse.If
$result->datais an Array, and it is just that you have no data (I’m not sure what Instagram would retrun if there were no results), add a check to see if the Array is empty before theforeach()loop.