Twitter Trends API weekly.json causing error "Cannot use object of type stdClass as array"

Posted by tucson on Stack Overflow See other posts from Stack Overflow or by tucson
Published on 2010-12-30T11:01:37Z Indexed on 2010/12/30 12:54 UTC
Read the original article Hit count: 239

Filed under:
|
|

I have the following PHP code:

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$URL);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        $result = curl_exec($ch);
        curl_close($ch);
        $obj = json_decode($result);

        foreach ($obj[0]->trends as $trend) echo utf8_decode($trend->name);

which works fine for URL #1 (into the variable $URL):

http://api.twitter.com/1/trends/1.json?exclude=hashtags

but causes an error "Cannot use object of type stdClass as array" for URL #2: http://api.twitter.com/1/trends/weekly.json?exclude=hashtags

I have searched for a while, but can't figure out a code to fix this and handle both URLs.

Any help would be much appreciated.

© Stack Overflow or respective owner

Related posts about php

Related posts about JSON