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