AJAX Tweets With JQuery and PHP
http://rainnerlins.com/blog/jquery-tweets
Tue, 01 Nov 2011 05:42:03 -0400
Visit Download Share Comment
This is a short tutorial showing how to use a custom PHP script and some JQuery AJAX action to fetch and display tweets from Twitter for a specific user on your site, or blog, without having to use complicated API keys, oAuth requests, or third-party applications.
Twitter allows you to send a "Server-Side" request to one of their URLs which can be used to get a user's timeline in JSON format. In this example, i am using JQuery.getJSON() to load data from a PHP file ( see below ) that will get the tweets code from Twitter, parse it and return a custom JSON object with all the pieces i need to format the tweets in any way i want once the request is done on the JQuery side.
I have provided all the files you'll need for this example, including the custom object for loading and formatting the HTML on the page with the download .rar file for this post. You can view the example page here: AJAX Tweets With JQuery.
Twitter Request URL
http://twitter.com/status/user_timeline/USERNAME.json
In order for this to work, your PHP installation needs to have the CURL library configured. If not, it only takes a few lines of code to install from a terminal ( Ubuntu/Debian ):
sudo apt-get install php5-curl service apache2 restart
PHP ( tweets.json.php )
// turn off any PHP error messages that might pop up
error_reporting( 0 );
// try to get the tweets
GetTweets( $_GET['uid'], $_GET['max'] );
// fetch the tweets and return raw JSON data for JS
function GetTweets( $uid, $max )
{
$uid = ( !empty( $uid ) ) ? trim( preg_replace( '/[^w-_]/i', ', $uid ) ) : ';
$max = ( !empty( $max ) ) ? $max : 5;
$url = 'http://twitter.com/status/user_timeline/'.$uid.'.json';
$out = array( 'status'=>'ok', 'user'=>array(), 'tweets'=>array() );
$num = 0;
// check for user name first
if( empty( $uid ) ){ $out['status'] = 'failed: no user id.'; }
else
{
// fetch the user tweets from Twitter
$c = curl_init();
curl_setopt( $c, CURLOPT_URL, $url );
curl_setopt( $c, CURLOPT_CONNECTTIMEOUT, 10 );
curl_setopt( $c, CURLOPT_RETURNTRANSFER, true );
$str = curl_exec( $c );
$obj = ( !empty( $str ) ) ? json_decode( $str ) : false;
curl_close( $c );
// check for errors
if( empty( $obj ) ){ $out['status'] = 'failed: could not fetch tweets.'; }
else
{
// build the user data first
$out['user'] = array(
'user_id' => $obj[0]->user->id_str,
'full_name' => $obj[0]->user->name,
'screen_name' => $obj[0]->user->screen_name,
'image_url' => $obj[0]->user->profile_image_url,
'description' => $obj[0]->user->description,
'location' => $obj[0]->user->location,
'created_at' => $obj[0]->user->created_at,
'web_url' => $obj[0]->user->url,
'twitter_url' => 'http://twitter.com/'.$obj[0]->user->screen_name,
'reply_link' => 'http://twitter.com/intent/tweet?text='.urlencode( '@'.$obj[0]->user->screen_name )
);
// build list of tweets for this user
foreach( $obj as $tweet )
{
// check for user specified limits
if( $num >= $max ){ break; } $num ;
// clean the tweet text and auto link hashes and user names
$txt = trim( strip_tags( html_entity_decode( urldecode( $tweet->text ) ) ) );
$txt = preg_replace( '/(https?://([w.-_/] ))/i', '<a title="Open link in new tab" href="$1" target="_blank">$1</a>', $txt ); // links
$txt = preg_replace( '/@([w_-] )/i', '<a title="Twitter page for: $1" href="http://twitter.com/$1" target="_blank">@$1</a>', $txt ); // people
$txt = preg_replace( '/#([w_-] )/i', '<a title="Search twitter for: $1" href="http://twitter.com/search?q=#$1" target="_blank">#$1</a>', $txt ); // hashes
// add to tweets array
$out['tweets'][] = array(
'tweet_text' => trim( $txt ),
'replied_to' => $tweet->in_reply_to_screen_name,
'posted_date' => $tweet->created_at,
'full_name' => $tweet->user->name
);
}
}
}
// return final array back as JSON data
echo trim( json_encode( $out ) );
}
Using JQuery to load the the PHP script
$(function()
{
$.getJSON( 'tweets.json.php', { uid:'@Google', max:10 }, function( data )
{
// data.status should say ok on success, or an error message
// data.user is an object containing all the user information
// data.tweets is an array of objects containing each tweet
});
});
User Comments
This section shows comments left for this post.
Sort:
Ascending |
Descending
Comment
Just fill in the form below to leave your comment as a guest.
Add a Comment
Blog Categories
Client Side Web Development Tips
Latest Work ( 12 )
This is a fully custom site, built on PHP and a custom CMS panel i did for adult..
Another previous version of this site that was built around 2010 to serve as a p..
This is a custom Flash, Portfolio mini-site i built for artist Aoife Hand. The c..
Tweets & Updates
My latest tweets will load here in just a second. In case they don't, you can head over to my twitter page @rainnerlins
http://rainnerlins.com/blog/jquery-tweets
This is a short tutorial showing how to use a custom PHP script and some JQuery ..