如何解码tweets.json
从 twitter 搜索 API 请求返回的字符串?我已经查看了类似问题的答案。这些答案确实显示了如何进行调用以及如何显示返回的数据,但这些答案并未涉及处理从tweets.json
API 调用返回的数据结构的问题。这是代码 - 它使用 twitter API。它请求搜索结果。
<?php
require_once('../TwitterAPIExchange.php');
$settings = array(
'oauth_access_token' => "......",
'oauth_access_token_secret' => "......",
'consumer_key' => "......",
'consumer_secret' => "......"
);
$requestMethod = 'GET';
//$url = "https://api.twitter.com/1.1/statuses/user_timeline.json"; // I can decode output from this
//$getfield = "?screen_name=J7mbo&count=5"; // I can decode output from this
$url = "https://api.twitter.com/1.1/search/tweets.json"; // I can NOT decode output from this
$getfield = "?q=%23J7mbo&result_type=recent"; // I can NOT decode output from this
$twitter = new TwitterAPIExchange($settings);
$string = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(); // from stackOverflow
$string = json_decode($string, $assoc = TRUE); // seems i cannot use json_decode for output from tweets.json
if ($string["errors"][0]["message"] != "")
{
echo "twitter error message:" . $string[errors][0]["message"];
exit();
}
foreach ($string as $items)
{
echo "tweet text =[". $items['text']."]<br />";
}
?>
如果我使用 twitter API 时间线调用,我可以使用json_decode
和访问$items['text']
每个返回的推文但我想使用 twitter API 搜索调用(tweets.json). json_decode
无法正确解码来自该搜索调用的数据,它只返回两个空的$items['text']
那么解码tweets.json
从 twitter API 请求返回的字符串的最佳方法是什么?