I want to echo twitter trends in a specific location. Say that the woeid = 1
This is my code:
// Twitter App OAuth
require_once('TwitterAPIExchange.php');
$settings = array(
'oauth_access_token' => "",
'oauth_access_token_secret' => "",
'consumer_key' => "",
'consumer_secret' => ""
);
// Get Trends
$url = "https://api.twitter.com/1.1/trends/place.json";
$requestMethod = "GET";
$getfield = "?id=1";
$twitter = new TwitterAPIExchange($settings);
$myfile = fopen("hashtags.json", "w") or die("Unable to open file!");
$txt = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
fwrite($myfile, $txt);
// echo Trends
$x = 0;
while($x <= 5) {
$news_url = 'hashtags.json';
$news = file_get_contents($news_url);
$news_array = json_decode($news, true);
$Hashtag = $news_array["trends"]["0"]["name"];
echo $Hashtag;
$x++;
}
The problem is: this line: $Hashtag = $news_array["trends"]["0"]["name"];
, it returns with Notice: Undefined index: trends in /home/user/public_html/Hashtag.php on line 30
error.
How can I fix that?
The problem was in this line:
$Hashtag = $news_array["trends"]["$x"]["name"];
It should be:
$Hashtag = $news_array["0"]["trends"]["$x"]["name"];
That was the format of the JSON file coming from twitter...