2017-10-20 8 views
0

特定の場所でTwitterの傾向をエコーし​​たいと思います。 = 1PHPを使用してTwitterトレンドを取得する方法

WOEIDこれは私のコードであることを言う:

// 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エラーで返されます。

どうすれば修正できますか?

$Hashtag = $news_array["trends"]["$x"]["name"]; 

はそれがあるべき:

+0

可能性のある重複しました[通知:未定義の変数]、[通知:未定義のインデックス]、および[通知:未定義のオフセット]](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined- index-and-notice-undef) – GrumpyCrouton

+0

解決策がまだ見つかりません。 –

+0

存在しないものにアクセスしようとしています。 'echo"

".print_r($news_array,true)."
"' – GrumpyCrouton

答えて

0

問題は、このラインにあった

$Hashtag = $news_array["0"]["trends"]["$x"]["name"]; 

IそのさえずりからJSONファイルのフォーマット...の

関連する問題