-1

Possible Duplicate:
Invalid argument supplied for foreach()

So, I have this function:

        <?php
        function get_instagram($user_id=15203338,$count=6,$width=190,$height=190){
            $url = 'https://api.instagram.com/v1/users/'.$user_id.'/media/recent/?access_token=13137.f59def8.1a759775695548999504c219ce7b2ecf&count='.$count;
            // Let's create a cache
            $cache = './wp-content/themes/multiformeingegno/instagram_json/'.sha1($url).'.json';
            if(file_exists($cache) && filemtime($cache) > time() - 1000){
                // If a cache file newer than 1000 seconds exist, use that
                $jsonData = json_decode(file_get_contents($cache));
            } else {
                $jsonData = json_decode((file_get_contents($url)));
                file_put_contents($cache,json_encode($jsonData));
            }
            $result = '<div id="instagram">'.PHP_EOL;
            foreach ($jsonData->data as $key=>$value) {
                $title = (!empty($value->caption->text))?' '.$value->caption->text:'...';
                $location = (!empty($value->location->name))?' presso '.$value->location->name:null;
                $result .= "\t".'<a class="fancybox" data-fancybox-group="gallery" href="'.$value->images->standard_resolution->url.'"><img src="'.$value->images->low_resolution->url.'" alt="'.$value->caption->text.'" width="'.$width.'" height="'.$height.'" /></a>
                <div style="display: none;">'.htmlentities($title, ENT_QUOTES, "UTF-8").'<br><em style="font-size:11px">Scattata il '.htmlentities(strftime('%e %B %Y alle %R', $value->caption->created_time + 7200)).' '.htmlentities($location).' (<a target="_blank" style="color:darkgrey" rel="nofollow" href="http://maps.google.com/maps?q='.htmlentities($value->location->latitude).',+'.htmlentities($value->location->longitude).'">mappa</a>)</em></div>'.PHP_EOL;
            }
            $result .= '</div>'.PHP_EOL;
            return $result;
        }
        echo get_instagram();
        ?>

I'm getting a lot of these errors: FastCGI sent in stderr: "PHP message: PHP Warning: Invalid argument supplied for foreach(). The line of the problem is

foreach ($jsonData->data as $key=>$value) {

What's wrong with that?

Thanks in advance guys! :)

Community
  • 1
  • 1
MultiformeIngegno
  • 6,959
  • 15
  • 60
  • 119
  • 1
    The error message is descriptive - what you think is array actually isn't. `var_dump($jsonData->data);` so see that yourself – zerkms Nov 02 '12 at 01:09
  • Uhm.. what should var_dump($jsonData->data); do? :) I'm a PHP noob, I actually copied portions of this function here and there... – MultiformeIngegno Nov 02 '12 at 01:12
  • @MultiformeIngegno It is the most basic form of debugging in PHP. It will dump the contents of whatever is passed to it - in this case, `$jsonData` is some object, and you _hope_ that its `data` property is an array. `var_dump()` will prove or disprove. – Michael Berkowski Nov 02 '12 at 01:17
  • @MultiformeIngegno: "I'm a PHP noob, I actually copied portions of this function here and there" --- and what is your aim? You won't learn anything in this case. – zerkms Nov 02 '12 at 01:18
  • Thanks Michael for the explanation and zerkms you're right.. I should study things in first place.. – MultiformeIngegno Nov 02 '12 at 01:20

2 Answers2

1

When you use json_decode you get an object. If you want an array you can use the second argument of json_decode which is a toggle for return an object or an array. true gives arrays.

You'll need to adapt your code to use the new array.

Another thing that might help, (not sure, because i don't know what's in the object), is to cast the object to an array (but that is a bit of an hack ;)):

foreach ((array) $jsonData->data as $key=>$value) {
Tim
  • 348
  • 2
  • 15
0

try the following before your foreach :

if(is_array($jsonData->data)){
    // Do the for each
} else {
    // It wasn't an array so do something else
    // Like an error message or w/e
}

This is useful when what you get in the json is not always an array.

If it's suppose to be an array, then use it only for debugging and do a var_dump($jsonData->data); in the else to see what you actually got

Hugo Dozois
  • 8,147
  • 12
  • 54
  • 58
  • Uhm.. I don't know much about PHP but this seems a bit dirty..? What is it supposed to do? :) – MultiformeIngegno Nov 02 '12 at 01:14
  • well it checks if the value is an array or not. It may be used for testing if you are actually suppose always get an array. But if what you get is not always an array its prolly the best solution – Hugo Dozois Nov 02 '12 at 01:15
  • Ok, the function seems to be working properly (like before). Now I'm waiting to see if I still get errors. :) – MultiformeIngegno Nov 02 '12 at 01:22
  • The error you had might have been caused by an error from the request. So you might have received a JSON with like only "error" in it which obviously wasn't an array. – Hugo Dozois Nov 02 '12 at 01:24