【问题标题】:Get trending topics from Twitter api从 Twitter api 获取热门话题
【发布时间】:2017-10-07 00:32:17
【问题描述】:

作为一个 php 菜鸟,我正在尝试解决 Twitter 的 api 问题。我想要做的是获取 Twitter 热门话题并将它们显示为网站上的列表。到目前为止我所拥有的:

<?php
require_once('TwitterAPIExchange.php');
 
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
    'oauth_access_token' => "xxxxxx",
    'oauth_access_token_secret' => "xxxxxxx",
    'consumer_key' => "xxxxxxxxxx",
    'consumer_secret' => "xxxxxxxxxxx"
);
$url = "https://api.twitter.com/1.1/trends/place.json";
$requestMethod = "GET";
$getfield = '?id=23424829&count=10';
$twitter = new TwitterAPIExchange($settings);
$string = json_decode($twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(),$assoc = TRUE);
if($string["errors"][0]["message"] != "") {echo "<h3>Sorry, there was a problem.</h3><p>Twitter returned the following error message:</p><p><em>".$string[errors][0]["message"]."</em></p>";exit();}
foreach($string as $items)
    {
        echo "Hashtag: ".$items['name']."<br />";
        
    }

?>

我可以在 JSON 中得到正确的结果并将它们解码为一个数组。但是,当我尝试使用 foreach 循环数组时,它会显示一个空白页面,只显示“Hashtag:”但没有结果。

解码后的 JSON 开头如下所示:

Array
(
    [0] => stdClass Object
        (
            [trends] => Array
                (
                    [0] => stdClass Object
                        (
                            [name] => #NakedAttraction
                            [url] => http://twitter.com/search?q=%23NakedAttraction
                            [promoted_content] => 
                            [query] => %23NakedAttraction
                            [tweet_volume] => 
                        )

这是我之前用来解码 JSON 的代码:

<?php
require_once('TwitterAPIExchange.php');
 
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
    'oauth_access_token' => "XXXXXXXXXX",
    'oauth_access_token_secret' => "XXXXXXXX",
    'consumer_key' => "XXXXXXXX",
    'consumer_secret' => "XXXXXXXXXX"
);
$url = "https://api.twitter.com/1.1/trends/place.json";
$requestMethod = "GET";
$getfield = '?id=23424829&count=10';
$twitter = new TwitterAPIExchange($settings);
$string = json_decode($twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(),$assoc = FALSE);
if($string["errors"][0]["message"] != "") {echo "<h3>Sorry, there was a problem.</h3><p>Twitter returned the following error message:</p><p><em>".$string[errors][0]["message"]."</em></p>";exit();}
echo "<pre>";
print_r($string);
echo "</pre>";

?>

改代码后

$string = json_decode($twitter->setGetfield($getfield) ->buildOauth($url, $requestMethod) ->performRequest(), TRUE);
var_dump($string);

我收到了这个

array(1) { [0]=&gt; array(4) { ["trends"]=&gt; array(49) { [0]=&gt; array(5) { ["name"]=&gt; string(16) "#NakedAttraction" ["url"]=&gt; string(46) "http://twitter.com/search?q=%23NakedAttraction" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(18) "%23NakedAttraction" ["tweet_volume"]=&gt; NULL } [1]=&gt; array(5) { ["name"]=&gt; string(13) "#hartaberfair" ["url"]=&gt; string(43) "http://twitter.com/search?q=%23hartaberfair" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(15) "%23hartaberfair" ["tweet_volume"]=&gt; NULL } [2]=&gt; array(5) { ["name"]=&gt; string(7) "#EBSFCU" ["url"]=&gt; string(37) "http://twitter.com/search?q=%23EBSFCU" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(9) "%23EBSFCU" ["tweet_volume"]=&gt; NULL } [3]=&gt; array(5) { ["name"]=&gt; string(8) "#4Blocks" ["url"]=&gt; string(38) "http://twitter.com/search?q=%234Blocks" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(10) "%234Blocks" ["tweet_volume"]=&gt; NULL } [4]=&gt; array(5) { ["name"]=&gt; string(11) "#LuchaTele5" ["url"]=&gt; string(41) "http://twitter.com/search?q=%23LuchaTele5" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(13) "%23LuchaTele5" ["tweet_volume"]=&gt; NULL } [5]=&gt; array(5) { ["name"]=&gt; string(11) "Diego Costa" ["url"]=&gt; string(45) "http://twitter.com/search?q=%22Diego+Costa%22" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(17) "%22Diego+Costa%22" ["tweet_volume"]=&gt; int(13267) } [6]=&gt; array(5) { ["name"]=&gt; string(11) "Wochenstart" ["url"]=&gt; string(39) "http://twitter.com/search?q=Wochenstart" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(11) "Wochenstart" ["tweet_volume"]=&gt; NULL } [7]=&gt; array(5) { ["name"]=&gt; string(17) "Deutsche Konzerne" ["url"]=&gt; string(51) "http://twitter.com/search?q=%22Deutsche+Konzerne%22" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(23) "%22Deutsche+Konzerne%22" ["tweet_volume"]=&gt; NULL } [8]=&gt; array(5) { ["name"]=&gt; string(7) "Session" ["url"]=&gt; string(35) "http://twitter.com/search?q=Session" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(7) "Session" ["tweet_volume"]=&gt; int(69911) } [9]=&gt; array(5) { ["name"]=&gt; string(18) "Schleswig-Holstein" ["url"]=&gt; string(46) "http://twitter.com/search?q=Schleswig-Holstein" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(18) "Schleswig-Holstein" ["tweet_volume"]=&gt; NULL } [10]=&gt; array(5) { ["name"]=&gt; string(7) "#CHEMID" ["url"]=&gt; string(37) "http://twitter.com/search?q=%23CHEMID" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(9) "%23CHEMID" ["tweet_volume"]=&gt; int(51187) } [11]=&gt; array(5) { ["name"]=&gt; string(19) "#GoodbyeDeutschland" ["url"]=&gt; string(49) "http://twitter.com/search?q=%23GoodbyeDeutschland" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(21) "%23GoodbyeDeutschland" ["tweet_volume"]=&gt; NULL } [12]=&gt; array(5) { ["name"]=&gt; string(16) "#BladeRunner2049" ["url"]=&gt; string(46) "http://twitter.com/search?q=%23BladeRunner2049" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(18) "%23BladeRunner2049" ["tweet_volume"]=&gt; int(33497) } [13]=&gt; array(5) { ["name"]=&gt; string(13) "#DWTStockholm" ["url"]=&gt; string(43) "http://twitter.com/search?q=%23DWTStockholm" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(15) "%23DWTStockholm" ["tweet_volume"]=&gt; int(22936) } [14]=&gt; array(5) { ["name"]=&gt; string(11) "#SallyYates" ["url"]=&gt; string(41) "http://twitter.com/search?q=%23SallyYates" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(13) "%23SallyYates" ["tweet_volume"]=&gt; int(247812) } [15]=&gt; array(5) { ["name"]=&gt; string(14) "#BlognBurger60" ["url"]=&gt; string(44) "http://twitter.com/search?q=%23BlognBurger60" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(16) "%23BlognBurger60" ["tweet_volume"]=&gt; NULL } [16]=&gt; array(5) { ["name"]=&gt; string(7) "#Pegida" ["url"]=&gt; string(37) "http://twitter.com/search?q=%23Pegida" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(9) "%23Pegida" ["tweet_volume"]=&gt; NULL } [17]=&gt; array(5) { ["name"]=&gt; string(5) "#lobo" ["url"]=&gt; string(35) "http://twitter.com/search?q=%23lobo" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(7) "%23lobo" ["tweet_volume"]=&gt; NULL } [18]=&gt; array(5) { ["name"]=&gt; string(7) "#usaswe" ["url"]=&gt; string(37) "http://twitter.com/search?q=%23usaswe" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(9) "%23usaswe" ["tweet_volume"]=&gt; NULL } [19]=&gt; array(5) { ["name"]=&gt; string(12) "#TheVampsVIP" ["url"]=&gt; string(42) "http://twitter.com/search?q=%23TheVampsVIP" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(14) "%23TheVampsVIP" ["tweet_volume"]=&gt; int(20397) } [20]=&gt; array(5) { ["name"]=&gt; string(9) "#LTW17nrw" ["url"]=&gt; string(39) "http://twitter.com/search?q=%23LTW17nrw" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(11) "%23LTW17nrw" ["tweet_volume"]=&gt; NULL } [21]=&gt; array(5) { ["name"]=&gt; string(14) "#NiemalsAllein" ["url"]=&gt; string(44) "http://twitter.com/search?q=%23NiemalsAllein" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(16) "%23NiemalsAllein" ["tweet_volume"]=&gt; NULL } [22]=&gt; array(5) { ["name"]=&gt; string(8) "#Juncker" ["url"]=&gt; string(38) "http://twitter.com/search?q=%23Juncker" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(10) "%23Juncker" ["tweet_volume"]=&gt; NULL } [23]=&gt; array(5) { ["name"]=&gt; string(6) "#mcb17" ["url"]=&gt; string(36) "http://twitter.com/search?q=%23mcb17" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(8) "%23mcb17" ["tweet_volume"]=&gt; NULL } [24]=&gt; array(5) { ["name"]=&gt; string(8) "#ltwsh17" ["url"]=&gt; string(38) "http://twitter.com/search?q=%23ltwsh17" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(10) "%23ltwsh17" ["tweet_volume"]=&gt; NULL } [25]=&gt; array(5) { ["name"]=&gt; string(16) "#TagderBefreiung" ["url"]=&gt; string(46) "http://twitter.com/search?q=%23TagderBefreiung" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(18) "%23TagderBefreiung" ["tweet_volume"]=&gt; NULL } [26]=&gt; array(5) { ["name"]=&gt; string(19) "#Presidentielle2017" ["url"]=&gt; string(49) "http://twitter.com/search?q=%23Presidentielle2017" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(21) "%23Presidentielle2017" ["tweet_volume"]=&gt; int(216803) } [27]=&gt; array(5) { ["name"]=&gt; string(7) "#GERRUS" ["url"]=&gt; string(37) "http://twitter.com/search?q=%23GERRUS" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(9) "%23GERRUS" ["tweet_volume"]=&gt; NULL } [28]=&gt; array(5) { ["name"]=&gt; string(9) "#freubier" ["url"]=&gt; string(39) "http://twitter.com/search?q=%23freubier" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(11) "%23freubier" ["tweet_volume"]=&gt; NULL } [29]=&gt; array(5) { ["name"]=&gt; string(7) "#SCFS04" ["url"]=&gt; string(37) "http://twitter.com/search?q=%23SCFS04" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(9) "%23SCFS04" ["tweet_volume"]=&gt; NULL } [30]=&gt; array(5) { ["name"]=&gt; string(6) "#dmb17" ["url"]=&gt; string(36) "http://twitter.com/search?q=%23dmb17" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(8) "%23dmb17" ["tweet_volume"]=&gt; NULL } [31]=&gt; array(5) { ["name"]=&gt; string(15) "#Frankreichwahl" ["url"]=&gt; string(45) "http://twitter.com/search?q=%23Frankreichwahl" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(17) "%23Frankreichwahl" ["tweet_volume"]=&gt; NULL } [32]=&gt; array(5) { ["name"]=&gt; string(25) "#SignOfTheTimesMusicVideo" ["url"]=&gt; string(55) "http://twitter.com/search?q=%23SignOfTheTimesMusicVideo" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(27) "%23SignOfTheTimesMusicVideo" ["tweet_volume"]=&gt; int(396869) } [33]=&gt; array(5) { ["name"]=&gt; string(17) "#muttertagswunsch" ["url"]=&gt; string(47) "http://twitter.com/search?q=%23muttertagswunsch" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(19) "%23muttertagswunsch" ["tweet_volume"]=&gt; NULL } [34]=&gt; array(5) { ["name"]=&gt; string(9) "#AnneWill" ["url"]=&gt; string(39) "http://twitter.com/search?q=%23AnneWill" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(11) "%23AnneWill" ["tweet_volume"]=&gt; NULL } [35]=&gt; array(5) { ["name"]=&gt; string(11) "#Polizeiruf" ["url"]=&gt; string(41) "http://twitter.com/search?q=%23Polizeiruf" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(13) "%23Polizeiruf" ["tweet_volume"]=&gt; NULL } [36]=&gt; array(5) { ["name"]=&gt; string(8) "#Framing" ["url"]=&gt; string(38) "http://twitter.com/search?q=%23Framing" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(10) "%23Framing" ["tweet_volume"]=&gt; NULL } [37]=&gt; array(5) { ["name"]=&gt; string(7) "#Tuchel" ["url"]=&gt; string(37) "http://twitter.com/search?q=%23Tuchel" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(9) "%23Tuchel" ["tweet_volume"]=&gt; NULL } [38]=&gt; array(5) { ["name"]=&gt; string(18) "#dasperfektedinner" ["url"]=&gt; string(48) "http://twitter.com/search?q=%23dasperfektedinner" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(20) "%23dasperfektedinner" ["tweet_volume"]=&gt; NULL } [39]=&gt; array(5) { ["name"]=&gt; string(6) "#Fritz" ["url"]=&gt; string(36) "http://twitter.com/search?q=%23Fritz" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(8) "%23Fritz" ["tweet_volume"]=&gt; NULL } [40]=&gt; array(5) { ["name"]=&gt; string(19) "#frenchelection2017" ["url"]=&gt; string(49) "http://twitter.com/search?q=%23frenchelection2017" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(21) "%23frenchelection2017" ["tweet_volume"]=&gt; int(52362) } [41]=&gt; array(5) { ["name"]=&gt; string(8) "#tasteup" ["url"]=&gt; string(38) "http://twitter.com/search?q=%23tasteup" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(10) "%23tasteup" ["tweet_volume"]=&gt; NULL } [42]=&gt; array(5) { ["name"]=&gt; string(16) "#Montagslaecheln" ["url"]=&gt; string(46) "http://twitter.com/search?q=%23Montagslaecheln" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(18) "%23Montagslaecheln" ["tweet_volume"]=&gt; NULL } [43]=&gt; array(5) { ["name"]=&gt; string(7) "#vfbaue" ["url"]=&gt; string(37) "http://twitter.com/search?q=%23vfbaue" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(9) "%23vfbaue" ["tweet_volume"]=&gt; NULL } [44]=&gt; array(5) { ["name"]=&gt; string(11) "#Kofferraum" ["url"]=&gt; string(41) "http://twitter.com/search?q=%23Kofferraum" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(13) "%23Kofferraum" ["tweet_volume"]=&gt; NULL } [45]=&gt; array(5) { ["name"]=&gt; string(15) "#ParisAgreement" ["url"]=&gt; string(45) "http://twitter.com/search?q=%23ParisAgreement" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(17) "%23ParisAgreement" ["tweet_volume"]=&gt; NULL } [46]=&gt; array(5) { ["name"]=&gt; string(7) "#cl2025" ["url"]=&gt; string(37) "http://twitter.com/search?q=%23cl2025" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(9) "%23cl2025" ["tweet_volume"]=&gt; NULL } [47]=&gt; array(5) { ["name"]=&gt; string(10) "#MTVAwards" ["url"]=&gt; string(40) "http://twitter.com/search?q=%23MTVAwards" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(12) "%23MTVAwards" ["tweet_volume"]=&gt; int(1306553) } [48]=&gt; array(5) { ["name"]=&gt; string(16) "#TattoosForShawn" ["url"]=&gt; string(46) "http://twitter.com/search?q=%23TattoosForShawn" ["promoted_content"]=&gt; NULL ["query"]=&gt; string(18) "%23TattoosForShawn" ["tweet_volume"]=&gt; int(31256) } } ["as_of"]=&gt; string(20) "2017-05-08T21:46:50Z" ["created_at"]=&gt; string(20) "2017-05-08T21:43:18Z" ["locations"]=&gt; array(1) { [0]=&gt; array(2) { ["name"]=&gt; string(7) "Germany" ["woeid"]=&gt; int(23424829) } } } }

你们中的任何一位聪明的编码僧侣能如此仁慈地帮助我吗?

【问题讨论】:

  • 你运行了什么代码来获得解码后的 JSON 的显示?只是想弄清楚你的对象/数组结构
  • 嗨,安迪,我编辑了我的初始帖子,并包含了用于解码 JSON 和打印数组的代码。我想解决方案对像你这样的人来说很容易。但是我,作为一个 PHP 菜鸟无法让它工作。任何帮助都非常受欢迎!
  • 我不完全确定,但我认为您正在尝试将结果用作数组,但它会作为对象返回。我不确定参数传递方法是否适用于 PHP,所以尝试将 $string = json_decode($twitter-&gt;setGetfield($getfield) -&gt;buildOauth($url, $requestMethod) -&gt;performRequest(),$assoc = TRUE); 更改为 $string = json_decode($twitter-&gt;setGetfield($getfield) -&gt;buildOauth($url, $requestMethod) -&gt;performRequest(), TRUE);
  • 感谢您的帮助,安迪!不幸的是,这没有用。结果是一样的。我只能看到打印的“Hashtag:”。其余为空。
  • 好的,假设它现在是一个数组,你不能在它的正确部分循环。在$string = json_decode($twitter-&gt;setGetfield($getfield) -&gt;buildOauth($url, $requestMethod) -&gt;performRequest(), TRUE); 之后请执行var_dump($string); 并查看页面源并向我们展示结果。 (请注意在 json_decode 调用中删除了“$assoc =”)

标签: php json twitter


【解决方案1】:

我不确定PHP是否支持这个参数传递或不这样改变:

$string = json_decode($twitter->setGetfield($getfield)
              ->buildOauth($url, $requestMethod)
              ->performRequest(), $assoc = TRUE);

到:

$string = json_decode($twitter->setGetfield($getfield)
              ->buildOauth($url, $requestMethod)
              ->performRequest(), TRUE);

这将确保 JSON 被转换为关联的数组。

然后您需要选择要循环的数组的右侧部分,因此将循环更改为:

foreach($string[0]["trends"] as $items)
{
    echo "Hashtag: ".$items['name']."<br />";

}

【讨论】:

  • 我试过了,没有任何变化。我还将整个内容从本地主机移动到服务器,看看这是否会导致问题。结果始终是没有结果的白页。我从没想过要抓住一些标签会这么难。
  • 不应该的..!如果你var_dump($string["trends"][0]["name"]);,你会得到什么
  • 嗨,安迪,很抱歉回复晚了。我的女朋友不得不去医院。当我插入 var_dump 时,我只会在白页上得到“NULL”。
  • 没问题。只需要在结果数组中找到正确的路径。试试var_dump($string[0]["trends"][0]["name"]);
  • 这个看起来好多了。输出为string(12) "#alsich10war",这是目前德国的第一个热门话题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-08
  • 2015-09-10
  • 2020-04-11
  • 2013-05-24
  • 2021-09-05
  • 2019-06-15
  • 2010-11-15
相关资源
最近更新 更多