1

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

3
  • 3
    Use var_dump($result->data) before the foreach structure to learn more about $result->data being an invalid argument for it.
    – greg0ire
    Oct 30, 2012 at 12:54
  • Most likely youre JSON failed to decode and its returning false. Can you put the text value of $result in your post? Have you run the text value of $result through something like jsonlint? Oct 30, 2012 at 12:55
  • Nothing wrong with the structure, it's likely that $result->data contains invalid data (i.e. is not an array, or is empty).
    – David Gard
    Oct 30, 2012 at 12:56

3 Answers 3

1

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 $userid and $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->data before 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 the json_decode() has failed and will show false.

If $result->data is 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 the foreach() loop.

<!-- This will output the contents of '$result->data' - let us know what it is -->
<?php var_dump($result->data)`) ?>

<?php if(!empty($result->data)): ?>
    <?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 ?>
<?php endif ?>
9
  • Parse error: syntax error, unexpected T_ENDIF in /home/mysite/index.php on line 45 Oct 30, 2012 at 13:08
  • So the problem is not with your code, it is with the value that is being returned by fetchData(). Just before the if() loop in the code above, add var_dump($result->data); - This will tell you what `$result->data contains, which I suspect will be nothing.
    – David Gard
    Oct 30, 2012 at 14:40
  • @DavidGard sorry, i don't understand you. Please, edit my code? Oct 30, 2012 at 14:46
  • Have made a edit to my code to show you where to put the var_dump().
    – David Gard
    Oct 30, 2012 at 14:56
  • Ok, so that is saying that it doesn't exist. On that line, replace $result->data with just $result and see what you get.
    – David Gard
    Oct 30, 2012 at 15:20
1
<?php
        // Supply a user id and an access token
        $userid = "*****";
        $accessToken = "*********";

        // 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/$userid/media/recent/?access_token=$accessToken");
        $result = json_decode($result);
    ?>

<?php if(!empty($result->data)): ?>
    <?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 ?>
<?php endif ?>

tried this on an on line compiler it works.

1
<?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 ?>

This is wrong syntax Replace it with:

<?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 } ?>
2
  • Can you please explain why this would help fix the issue?
    – John Odom
    Jun 3, 2015 at 16:17
  • See carefully the code I posted last. The problem is at the foreach syntax.
    – Cosmos
    Jun 3, 2015 at 17:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.