0

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"
      ...

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

And if I'm looping through them, how do I get the data? I'm using the foreach($json_output as $tweets) { as in the code example...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.