0

I'm trying to get Twitter's trending topics for an area using PHP but I'm a PHP noob. This is the code using

    $woeid = 2424766;
    $url= 'https://api.twitter.com/1.1/trends/place.json?id='.$woeid;
    $trends = $twitter->get($url);
    foreach ($trends->trends as $key => $trend) {
        <?=$trend->name; ?><br>
    }

but when I run the code I get an error on the page stating "Warning: Invalid argument supplied for foreach() in ..line blahblah.."
Note that I based my code off of a search function that I successfully implemented. I'm not sure why this approach isn't working though.

How can I fix this and display a list of trending topics?

user144602
  • 11
  • 2
  • 1
    `foreach` expects an array to iterate through. Echo or print out `$trends->trends` to see what you're trying to loop through. – cteski May 04 '17 at 19:10
  • @cteski when I print $trends->trends, nothing shows up. Do you know where I went wrong with the 'get' call then? – user144602 May 04 '17 at 19:22

1 Answers1

1

I figured it out guys!

This was the resulting code that worked

    <?php
    $woeid = 2424766;
    $url= 'https://api.twitter.com/1.1/trends/place.json?id='.$woeid;
    $trends = $twitter->get($url);
    foreach ($trends[0]->trends as $trend) { ?>
            <?=$trend->name; ?><br>
    <?php } ?>
user144602
  • 11
  • 2