Get Twitter Feed in PHP Associative Array

(Taken from ServerSideup)

The Twitter API uses REST as it’s way to transfer information back to the user in the form of JSON.  I ran into a situation recently where I needed to grab the 5 newest tweets and embed them into a website using PHP.  Using the built in PHP json_decode function, along with the CURL functions built into PHP, this process was extremely simple.  The follow code shows how to connect to the Twitter API and grab the information.  I also provided a simple while loop that prints out the tweet and the time it was tweeted.  When running this script, A TON more information gets returned, but these are the only fields that I needed.  Feel free to run a print_r function on the return variable to see all of the other information that is returned.

//Set the Twitter Username
$twitterUsername = "serversideup";
//Number of tweets to grab
$numberOfTweets = 5;
//Open up the CURL connection
        $connectionSession = curl_init();
//Set the url that will be receiving the data.
//URL is from : https://dev.twitter.com/docs/api/1/get/statuses/user_timeline

        $url = 'https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name='.$twitterUsername.'&count='.$numberOfTweets;

//Set the CURL option with the appropriate URL
//For more information about the curl_setopt function visit: http://php.net/manual/en/function.curl-setopt.php.
curl_setopt($connectionSession, CURLOPT_URL, $url);

//Set the authentication.  Since this is RESTful it's just basic authentication
curl_setopt($connectionSession, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

//Other basic CURL settings
curl_setopt($connectionSession, CURLOPT_HTTPGET, 1);
curl_setopt($connectionSession, CURLOPT_HEADER, false);

//Really Important! You must set that you accept JSON which is what the Twitter API returns
curl_setopt($connectionSession, CURLOPT_HTTPHEADER, array('Accept: application/json'));

//Returns any errors
curl_setopt($connectionSession, CURLOPT_RETURNTRANSFER, true);

//Response receives the JSON text and executes the CURL session
$response = curl_exec($connectionSession);

//Closes the CURL session
curl_close($connectionSession);

//Now we load the JSON into an associative array.  The first parameter is the response we got from the CURL session and the second boolean is for the decode to return an associative array.
$tweets = json_decode($response, true);

//I ran a count on the tweet array even though we set it to 5 just so we don't get an array index out of bounds error in the loop.
        $tweetCount = count($tweets);
        $counter = 0;

//Iterates through all of the tweets and prints out all of the tweets received and the date they were created at.
        while($counter < $tweetCount){
            echo $tweets[$counter]["text"].'<br>'.$tweets[$counter]["created_at"].'<hr>';
            $counter++;
        }

Like I said, feel free to run the print_r function on the $tweets variable to view all of the returned data!

Advertisements

About berrymanchris

I recently graduated from University of Wisconsin - Whitewater and I work as a Senior Analyst at PANGEA Studios at the University in Whitewater, WI.

Posted on August 1, 2012, in Blog and tagged , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s