从Twitter api获取热门话题

时间:2017-05-08 21:04:34

标签: php json twitter

作为一名php noob,我试图解决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 />";
        
    }

?>
&#13;
&#13;
&#13;

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

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

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

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

&#13;
&#13;
<?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>";

?>
&#13;
&#13;
&#13;

将代码更改为

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

我收到了这个

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

你们中任何一位明智的编码僧人都可以如此仁慈地帮助我吗?

1 个答案:

答案 0 :(得分:0)

我不确定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 />";

}