0

Hello all having some trouble accessing elements from a returned JSON from Twitter. I'm trying to retrieve about 18 tweets from 6 different account and display them in chronological order, but I can't figure out how to access the elements in the array. I've tried using json_decode(), tried foreach loop and a for loop. Basically I out of all of the following, all I need is the following elements: text, user->screen_name, user->profile_image_url, and created_at. Reason being when I upload the php to a website, the app I'm building times out and doesn't receive the data, so I'm trying to make the return a lot smaller by only getting back what is needed. Anywho, here is a sample of 1 tweet I'm receiving back...

[{"in_reply_to_status_id":null,"created_at":"Thu Aug 25 01:50:07 +0000 2011","geo":null,"user":{"profile_background_tile":true,"protected":false,"url":"http:\/\/www.backpocketmemory.com","name":"Back Pocket Memory","default_profile":false,"friends_count":1115,"profile_link_color":"0084B4","default_profile_image":false,"show_all_inline_media":false,"contributors_enabled":false,"following":false,"geo_enabled":false,"utc_offset":-28800,"profile_sidebar_border_color":"C0DEED","description":"We're a hard rock\/pop band from Burbank, CA. We love you. facebook.com\/backpocketmemory @bpmchris @bpmguitar @bpmsixstring @bpmbass @bpmdrummer\r\n","profile_background_image_url_https":"https:\/\/si0.twimg.com\/profile_background_images\/236635469\/60509_430737311786_16094846786_5566606_2050188_n.jpg","time_zone":"Pacific Time (US & Canada)","created_at":"Mon Jul 27 20:58:35 +0000 2009","profile_image_url":"http:\/\/a2.twimg.com\/profile_images\/1320311803\/STICKER_IDEA_1_normal.jpg","is_translator":false,"profile_use_background_image":true,"profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/1320311803\/STICKER_IDEA_1_normal.jpg","follow_request_sent":false,"statuses_count":2238,"profile_background_color":"C0DEED","screen_name":"bckpcktmmry","profile_background_image_url":"http:\/\/a3.twimg.com\/profile_background_images\/236635469\/60509_430737311786_16094846786_5566606_2050188_n.jpg","favourites_count":6,"id_str":"60708421","lang":"en","verified":false,"profile_text_color":"333333","location":"Burbank, CA","id":60708421,"listed_count":19,"notifications":false,"profile_sidebar_fill_color":"DDEEF6","followers_count":683},"in_reply_to_user_id":null,"truncated":false,"coordinates":null,"favorited":false,"possibly_sensitive":false,"in_reply_to_status_id_str":null,"in_reply_to_screen_name":null,"source":"\u003Ca href=\"http:\/\/www.facebook.com\/twitter\" rel=\"nofollow\"\u003EFacebook\u003C\/a\u003E","in_reply_to_user_id_str":null,"id_str":"106543849266098176","id":106543849266098176,"contributors":null,"place":null,"retweeted":false,"retweet_count":2,"text":"Want to pick up tix for Rob's Bday Show next week? Come visit us at rehearsal. 156 W Providencia in Burbank.... http:\/\/t.co\/oNvw5I6"}]

Any help is appreciated!

2
  • Can you post a more complete sample somewhere? Commented Aug 26, 2011 at 23:08
  • Edited the OP to add the "]" at the end of the JSON that is there but didn't appear when I pasted it. @sberry2A a more comlete sample of what, my PHP code or the JSON return? Commented Aug 26, 2011 at 23:30

3 Answers 3

1

Your JSON decodes fine:

array (
  0 => 
  array (
    'in_reply_to_status_id' => NULL,
    'created_at' => 'Thu Aug 25 01:50:07 +0000 2011',
    'geo' => NULL,
    'user' => 
    array (
      'profile_background_tile' => true,
      'protected' => false,
      'url' => 'http://www.backpocketmemory.com',
      'name' => 'Back Pocket Memory',
      'default_profile' => false,
      'friends_count' => 1115,
      'profile_link_color' => '0084B4',
      'default_profile_image' => false,
      'show_all_inline_media' => false,
      'contributors_enabled' => false,
      'following' => false,
      'geo_enabled' => false,
      'utc_offset' => -28800,
      'profile_sidebar_border_color' => 'C0DEED',
      'description' => 'We're a hard rock/pop band from Burbank, CA. We love you. facebook.com/backpocketmemory @bpmchris @bpmguitar @bpmsixstring @bpmbass @bpmdrummer
',
      'profile_background_image_url_https' => 'https://si0.twimg.com/profile_background_images/236635469/60509_430737311786_16094846786_5566606_2050188_n.jpg',
      'time_zone' => 'Pacific Time (US & Canada)',
      'created_at' => 'Mon Jul 27 20:58:35 +0000 2009',
      'profile_image_url' => 'http://a2.twimg.com/profile_images/1320311803/STICKER_IDEA_1_normal.jpg',
      'is_translator' => false,
      'profile_use_background_image' => true,
      'profile_image_url_https' => 'https://si0.twimg.com/profile_images/1320311803/STICKER_IDEA_1_normal.jpg',
      'follow_request_sent' => false,
      'statuses_count' => 2238,
      'profile_background_color' => 'C0DEED',
      'screen_name' => 'bckpcktmmry',
      'profile_background_image_url' => 'http://a3.twimg.com/profile_background_images/236635469/60509_430737311786_16094846786_5566606_2050188_n.jpg',
      'favourites_count' => 6,
      'id_str' => '60708421',
      'lang' => 'en',
      'verified' => false,
      'profile_text_color' => '333333',
      'location' => 'Burbank, CA',
      'id' => 60708421,
      'listed_count' => 19,
      'notifications' => false,
      'profile_sidebar_fill_color' => 'DDEEF6',
      'followers_count' => 683,
    ),
    'in_reply_to_user_id' => NULL,
    'truncated' => false,
    'coordinates' => NULL,
    'favorited' => false,
    'possibly_sensitive' => false,
    'in_reply_to_status_id_str' => NULL,
    'in_reply_to_screen_name' => NULL,
    'source' => '<a href="http://www.facebook.com/twitter" rel="nofollow">Facebook</a>',
    'in_reply_to_user_id_str' => NULL,
    'id_str' => '106543849266098176',
    'id' => 106543849266098176,
    'contributors' => NULL,
    'place' => NULL,
    'retweeted' => false,
    'retweet_count' => 2,
    'text' => 'Want to pick up tix for Rob's Bday Show next week? Come visit us at rehearsal. 156 W Providencia in Burbank.... http://t.co/oNvw5I6',
  ),
)

Echo all the user's avatars, names, and a date when they joined:

$data = json_decode($twitter_response);
foreach($data as $d)
{
    echo '<img src="' . $d->user->profile_image_url . '" /> ' . $d->user->screen_name . ' (since ' . $d->user->created_at . ')<br />';
}

If you are using an associative array as your json_decode output, make sure you reference the appropriate keys, like so:

foreach($data as $d)
{
    echo '<img src="' . $d['user']['profile_image_url'] . '" /> ' . $d['user']['screen_name'] . ' (since ' . $d['user']['created_at'] . ')<br />';
}

Working code: http://pastie.org/2436307 (removed single quotes in some values - just escape them or convert them when you decode/reference)

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

3 Comments

Seems to be decoding fine, I threw our code in my project to see if it would work, but it doesn't. Even tried just $d->created_at, but nothing. Strange. Thanks for the help, I'm leaving for work and will check it out more closely when I get there.
See updated code, depending on your json_decode flags, you might need the associative array method.
It does work with the $d->created_at. I was in a rush to get to work and didn't notice I was trying to decode my array variable using two $ signs. Took it out, works like a charm, thanks for your help!
0

Use http://json.parser.online.fr/ to verify that the JSON you are trying to json_decode is valid. That's the main function you need to use and it will return an array that you can use normally. Most likely your JSON is invalid and you'll need to get that cleaned up first.

2 Comments

If you took two seconds and pasted his JSON into JSONLint.com you'd see it's valid :)
I obviously missed a character or something when I did that in the first place, because it wasn't valid when I tried. Or, judging by @Narf's answer and the associated comment, it wasn't valid when I posted this answer.
0

There's no closing bracket, and thus it's not valid JSON data. Put ] at the end of the string and it parses just fine with json_decode();

1 Comment

Thanks, it actually is there, just didn't tag along when I pasted for whatever reason, fixed it in the OP.

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.