3

So I'm pulling down a user's tweet steam in JSON format via PHP. I'd like to decode it into an associative array or at least some more usable fashion rather than a string so that I can maneuver through it.

I've been reading like mad about json_decode, but for me it seems like when I use it, before and after, the contents of the file is still being detected as one long string. Can anyone help me figure out what I am doing wrong?

$url = "http://twitter.com/status/user_timeline/" . $username . ".json?count=" . $count . "&callback=?";    

// $url becomes "http://twitter.com/status/user_timeline/steph_Rose.json?count=5&callback=?";   
        $contents = file_get_contents($url);
        $results = json_decode($contents, true);

        echo "<pre>";
        print_r($results);
        echo "</pre>";

        echo gettype($results); // this returns string
5
  • Have you checked what $contents is like? I.e. the content of $contents? :) Commented Jan 26, 2010 at 21:16
  • json_last_error() returns JSON_ERROR_SYNTAX. Commented Jan 26, 2010 at 21:17
  • Yeah, it's a long long string. I just figured out the problem, though! Commented Jan 26, 2010 at 21:17
  • Alix, I will have to look into that Error. I do not know what it means. Commented Jan 26, 2010 at 21:17
  • See here for json_last_error() :php.net/manual/en/function.json-last-error.php If it is a syntax error than Twitter is not delivering valid JSON. Commented Jan 26, 2010 at 21:19

4 Answers 4

7

With callback in the URL, you get a string back that is wrapped in parenthesis ( ) (excerpt of the string):

([{"in_reply_to_user_id":  /* ...more data here...*/ }]);

This is not valid JSON.

Without callback, the result is only wrapped in [ ] which is valid:

 [{"in_reply_to_user_id":  /* ...more data here...*/ }]
Sign up to request clarification or add additional context in comments.

1 Comment

So I guess a callback is required for javascript due to it being client side and needing a trigger, but in PHP file_get_contents() will execute and the next step will not until the data has been read and stored. Just trying to figure out when to use the callback and when not to. As soon as I took it off, it worked. Thanks :) Checked as correct and voted up.
4

Ditch the &callback=? in the url.

2 Comments

Yep that's exactly what I ended up doing. I understand callbacks, but why would that mess this up?
Because the returned string had parenthesis around it. That's just simply, invalid json. There's no need/purpose for them in json, so they aren't included in the definition. Keep in mind, json is not exactly the same as javascript.
3

I was used to parsing JSON using the jQuery library, so I had the &callback=? at the end of the URL.

It seems as if I take this off, that json_decode() has no problem converting the data, then, to an array.

If anyone knows the reason why this would be, I would love to know.

Long story short, it works!!

Comments

3
   $url = "http://twitter.com/status/user_timeline/" . $username . ".json?count=" . $count;

remove the callback so your json is json and not jsonp, jsonp breaks on decoding

Comments

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.