0

How do you decode the tweets.json string returned from a twitter search API request? I have looked through answers to similar questions. Those answers do show how to make a call, and how to display the data returned, but those don't deal with the issue of dealing with the structure of the data that is returned from the tweets.json API call. Here's the code - it uses the twitter API. It requests search results.

<?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 />";
}
?>

If I was using a twitter API timeline call, I could use json_decode and access $items['text'] for each of the returned tweets But I want to use the twitter API search call (tweets.json). json_decode does not properly decode the data from this search call, it only returns two empty $items['text']

So what's the best way to decode the tweets.json string returned from a twitter API request?

Bruce
  • 1,647
  • 4
  • 20
  • 22
  • can you `var_dump($string)` before you try to `json_decode` it, and post the result. – Jonathan Jun 16 '15 at 03:38
  • Stack Overflow allows only small replies, but here it is with a lot cut out of the middle string(68016) "{"statuses":[{"metadata":{"iso_language_code":"en","result_type":"recent"},"created_at":"Tue Jun 16 05:30:32 +0000 2015","id":610680840767418368,"id_str":"610680840767418368","text":"RT *telegraph_sport: Lay off *PaulGallen13 his explosive presser is exactly what #NRL needed #Origin *BuzzRothfield http:\/\/t.co\/2UOtKnxaI8 \u2026","source":"\u003ca href=\"http:\/\/twitter.com\/download\/iphone\" rel=\"nofollow\"\u .... tities=1","count":15,"since_id":0,"since_id_str":"0"}}" – Kevin Pollard Jun 16 '15 at 05:49
  • Please note that I had to replace all the @ characters with * because stackoverflow thought I was trying to notify a bunch of users. There were no * characters so you can, if you like, replace all the * in my reply with @ and you will undo. – Kevin Pollard Jun 16 '15 at 05:53
  • Thanks @Augwa. I have found a way forward - see my comment below – Kevin Pollard Jun 17 '15 at 08:32

2 Answers2

0

You need to iterate through the $items array and get the text property off of there.

    foreach ($items as $item) {
       echo "tweet text =[". $item['text']."]<br />";
    }
Almazik G
  • 1,066
  • 1
  • 9
  • 21
  • I just tried your code. from what you've post, you're trying to get the `text` property not from the actual tweet object but from 'collection' of tweets. I presume I might have changed that and tried to fetch it from the actual tweet object. in that case, your query string looks wierd, it returns nothing. try to change it something like 'dog' and you'll see the results comign – Almazik G Jun 16 '15 at 05:51
  • I have tried it with other search results, and still get two empty $items['text']. You can see some of the output my reply (above) to Augwa. – Kevin Pollard Jun 16 '15 at 06:42
  • I still think you're missing my point. You need to have 2 `foreach` statements. right after you do `foreach ($string as $items)` do the second loop `foreach ($items as $item)` and only then try to display the text `echo "tweet text =[". $item['text']."]
    ";` do you do like that?
    – Almazik G Jun 16 '15 at 06:49
  • Thanks Andrew. Now I get your point. The 2 `foreach` statements statements seem to work (though they produce a few extra incorrect `$item['text']` values at the end). – Kevin Pollard Jun 16 '15 at 11:24
  • Thanks @AndrewMalinnkov. See the answer I provided below. – Kevin Pollard Jun 17 '15 at 08:34
0

After examining the data I noticed that is consisted of two JSON encoded strings, named statuses and search_metadata. I extracted the statuses string and was able to decode it using json_decode.