Twitter Trends API weekly.json causing error "Cannot use object of type stdClass as array"
- by tucson
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.