How to parse JSON (from Twitter) with PHP??

Zenax

Active Member
Messages
1,377
Reaction score
4
Points
38
Hi,

I assume that since the re-design of the forums, this is the best place to put it.

I am building a small script with PHP, that fetches from the Twitter API. I have managed to get the JSON output, but now am at a loss as to how to process the information I receive. I was wondering if you could look at the code I have below and help me to display it properly in HTML and CSS format??

PHP:
<?php

    session_start();

    require_once("twitteroauth-master/twitteroauth/twitteroauth.php");

    $account                         =        '';

    function get_Tweets($account) {
        $noTweets                    =        10;

        $api_key                    =        '';
        $api_secret                    =        '';

        $access_key                 =         '';
        $access_key_secret            =        '';

        function new_twitter_favourite_list($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) {
            $connect_to_twitter        =        new TwitterOAuth($cons_key,$cons_secret,$oauth_token,$oauth_token_secret);
            return $connect_to_twitter;
        }

        $connect_to_twitter = new_twitter_favourite_list($api_key, $api_secret, $access_key, $access_key_secret);
        $tweets = $connect_to_twitter->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$account."&count=".$noTweets);

        return $tweets;

        foreach($tweets as $tweet) {
            $status         =     $tweet->text;
            $tweetTime        =     $tweet->created_at;
            $tweetId         =     $tweet->id_str;

            $output             =      '<li>'.$status.'<a style="font-size:85%" href="http://twitter.com/'.$account.'/statuses/'.$tweetId.'">'. $tweetTime .'</a></li>';

            echo $output;
        }

    }

Any help greatly appreciated!! (Please ignore the terrible indentation in my code, it didn't seem to paste right from Sublime into this post.)
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
All you seem to be missing is a json_decode() call between getting the JSON and iterating over the tweets. json_decode() will convert the JSON text into a nested PHP array/hash/dictionary thingy for you.
 

Zenax

Active Member
Messages
1,377
Reaction score
4
Points
38
So instead of echoing the outut as $ouput I should do something like
PHP:
json_decode($output);
?? and when it comes out as an array how would I get certain details out of said array??
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
No, $tweets needs to be JSON-decoded:
PHP:
$tweets = json_decode($connect_to_twitter->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$account."&count=".$noTweets));
You already have the code in place to iterate through the array (it's what that foreach($tweets as $tweet){...} loop is all about). Oh, you'll need to get rid of the return $tweets; line, since you're not taking that variable outside of the get_Tweets() function.
 
Top