【发布时间】:2018-04-01 23:38:33
【问题描述】:
我想在特定位置呼应 Twitter 趋势。说woeid = 1
这是我的代码:
// 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++;
}
问题是:这一行:$Hashtag = $news_array["trends"]["0"]["name"];,它返回 Notice: Undefined index: trends in /home/user/public_html/Hashtag.php on line 30 错误。
我该如何解决这个问题?
【问题讨论】:
-
我还是找不到解决办法..
-
您正在尝试访问不存在的内容。做
echo "<pre>".print_r($news_array,true)."</pre>" -
成功了!谢谢你最好的:D
标签: php arrays json api twitter