store an array value to a new variable using it's key - twitter api

281 views Asked by At

I'm trying to write code that will return the past 100 tweets that contain the current trending hashtags on twitter. First I get the contents of the current trends and isolate just trending hashtags:

$json_output=json_decode(file_get_contents("https://api.twitter.com/1/trends/23424977.json"),true);
print_r($json_output);
foreach($json_output[0]['trends'] as $trend) {
    if ($trend['name'][0] === '#') {
       echo $trend['name'];
       $hashtag == $trend['name'];
    }
}

But rather than echo the trend['name'], I want to use it to search using the twitter search method. By adding something like this inside the if statement:

 $past_uses = json_decode(file_get_contents("http://search.twitter.com/search.json?q="$hashtag"&rpp=100&include_entities=true&result_type=popular"),true);

But the variable $hashtag isn't being defined properly and I don't know why. (When I try to echo $hashtags, to check that it's storing the proper value, it doesn't print anything.) So, what should I change so that the value of $trend['name'] can be used in the URL for the search method in order to get the past tweets that included the trending hashtag?

Thank you!

1

There are 1 answers

1
Paul Armstrong On

You're doing a comparison instead of assigning $hashtag.

$hashtag == $trend['name'];

That's basically just saying false inline. Instead, use a single equal sign:

$hashtag = $trend['name'];

Also, for your $past_uses, make sure you concatenate the string with dots properly:

 $past_uses = json_decode(
    file_get_contents("http://search.twitter.com/search.json?q=" . $hashtag . "&rpp=100&include_entities=true&result_type=popular"),
 true);