Decoding JSON feed via PHP from Twitter not working?

So, I am pulling out a custom JSON tweeter via PHP. I would like to decode it into an associative array, or at least a more convenient way, rather than a string, so that I can maneuver through it.

I read like crazy about json_decode, but it seems to me that when I use it, before and after, the contents of the file still show up as one long line. Can someone help me figure out what I'm 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
+3
source share
4 answers

With callbackin the url you get a string that is wrapped in a parenthesis ( )(excerpt from the string):

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

JSON.

callback [ ], :

 [{"in_reply_to_user_id":  /* ...more data here...*/ }]
+7

& callback =? URL.

+4

I used to parse JSON using the jQuery library, so I had & callback =? at the end of the url.

It seems that I am shooting this that json_decode () has no problem converting the data and then into an array.

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

In short, it works !!

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

remove callback so your json is json and not jsonp, jsonp aborts decoding

+3
source

Source: https://habr.com/ru/post/1730114/


All Articles