0

如何解码tweets.json从 twitter 搜索 API 请求返回的字符串?我已经查看了类似问题的答案。这些答案确实显示了如何进行调用以及如何显示返回的数据,但这些答案并未涉及处理从tweets.jsonAPI 调用返回的数据结构的问题。这是代码 - 它使用 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 请求返回的字符串的最佳方法是什么?

4

2 回答 2

0

您需要遍历$items数组并从text那里获取属性。

    foreach ($items as $item) {
       echo "tweet text =[". $item['text']."]<br />";
    }
于 2015-06-16T04:56:12.490 回答
0

在检查数据后,我注意到它由两个 JSON 编码的字符串组成,分别命名为statusessearch_metadata. 我提取了statuses字符串并能够使用json_decode.

于 2015-06-17T08:57:58.993 回答