andrebruton
andrebruton

Reputation: 2376

Reading Twitter JSON data using php for API 1.1

I had to make a mad scramble to get all my Twitter feeds going again when API 1.0 was closed. It took a while but I managed to get the GET statuses/user_timeline working again. Now I'm attempting to use the search and I'm not being able to read the results. The JSON file is returning the correct data.

Here is my PHP code:

$code = $tmhOAuth->request('GET', $tmhOAuth->url('1.1/search/tweets'), array(
     'q' => '%23'.$tagzname,
     'since_id' => $num,
     'lang' => 'en',
     'count' => '2'));

 $response = $tmhOAuth->response['response'];

 var_dump(json_decode($response, true));
 echo "<br><br><br><br>";

 $json_output = json_decode($response, true);

 foreach($json_output as $tweets) {

   // $tmsgid   = $tweets['statuses']['metadata']['id_str'];
   $tmsgid   = $tweets['statuses']['id_str'];
   // $tmsgid   = $tweets['metadata']['id_str'];
   // $tmsgid   = $tweets['id_str'];
   echo 'Msg ID:' . $tmsgid . '<br>';

I just can't get the Msg ID output. What am I missing?

The JSON data looks like this:

array(2) {
  ["statuses"]=>
  array(2) {
    [0]=>
    array(23) {
      ["metadata"]=>
      array(2) {
        ["result_type"]=>
        string(6) "recent"
        ["iso_language_code"]=>
        string(2) "en"
      }
      ["created_at"]=>
      string(30) "Thu Jun 13 07:19:30 +0000 2013"
      ["id"]=>
      float(3.4507795373608E+17)
      ["id_str"]=>
      string(18) "345077953736081409"
      ...

Upvotes: 0

Views: 4722

Answers (1)

Tobias Golbs
Tobias Golbs

Reputation: 4616

Inside of the statuses is an array with two entries. So you have to define which one you want to show.

foreach($json_output as $tweets) {
    $tmsgid = $tweets['statuses'][0]['id_str'];
}

EDIT: The first one was wrong!

foreach($json_output['statuses'] as $tweets) {
    $tmsgid = $tweets['id_str'];
}

This should be the correct way to loop through your statuses. I don't know what the second element in your root-array is.

Upvotes: 2

Related Questions