Get Latest Twitter Status Easily with PHP

Recently I wanted to display my Last Tweet on a website, but you may have noticed that the old Twitter API v1.0 doesn’t work anymore, the new update 1.1 requires authentication in order to interact with Twitter, which means there is no straight forward way of doing this without obtaining Twitter API keys.

Getting Twitter API Keys

So, If you do not have Twitter application created, you need to create one here. Once it is created, click your application name and click “API Keys” tab on your application details page. There you should find all API key and API secret. If you scroll down a bit you will also see your Access Token and Secret. twitter_api_keys twitter_access_token

PHP library

Go to Github and download Abraham Williams’s popular TwitterOAuth PHP library, this library is required to make HTTP requests to Twitter, we just need two files from twitteroauth folder from this library, just copy twitteroauth into your project.

PHP Code

Here’s the final code that goes into your website project. Copy your API Keys and replace values below where indicated.
PHP
1234567891011121314151617181920212223242526
<?php
include_once('twitteroauth/twitteroauth.php');

$twitter_customer_key 			= 'JdmrAiQk2mx8v3tvhDEA';
$twitter_customer_secret 		= 'uQ7imQSv6hirqQr9Nt5mksCdUCVAfk5srF0Mk3vo';
$twitter_access_token 			= '17587879-mftRUoqaPQj2OLZdu2Y08qY9vHRJfM7hE2yo87Y3d';
$twitter_access_token_secret 	= 'jFQyQ64PAEnyBVCWYPgJdVEGr3X0RpoCldgkyLUW5A';

$connection = new TwitterOAuth($twitter_customer_key, $twitter_customer_secret, $twitter_access_token, $twitter_access_token_secret);

$my_tweets = $connection->get('statuses/user_timeline', array('screen_name' => 'saaraan', 'count' => 1));

echo '<div class="twitter-bubble">';
if(isset($my_tweets->errors))
{			
	echo 'Error :'. $my_tweets->errors[0]->code. ' - '. $my_tweets->errors[0]->message;
}else{
	echo makeClickableLinks($my_tweets[0]->text);
}
echo '</div>';

//function to convert text url into links.
function makeClickableLinks($s) {
  return preg_replace('@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@', '<a target="blank" rel="nofollow" href="$1" target="_blank">$1</a>', $s);
}
?>
That’s it, the code should start pulling your latest tweet into your website. You can download the sample file below which also includes Abraham Williams twitter PHP library.  Download
  • 13 Comments

    Add Comment
    • Kuldeep
      it is not displaying my tweets it is displaying '''@Dr_Abbas014 hahahaha sirji afwain hn sari :p''''
    • David Lewis
      Awesome. Simple. One question, is there an easy to to exclude @ replies... i.e. to only pull original stand alone tweets not bits of a conversation.
    • Leticia Rezende
      I wanted more than 1 tweets so, what I tried (and it worked) was doing a foreach. $hein = array(0,1,2); foreach ($hein as &$opa) { $content = $my_tweets[$opa]->text; echo $content.''; };
    • James
      I've followed your instructions and I'm getting the following error: Parse error: syntax error, unexpected '[' in /home/content/src/TwitterOAuth.php on line 131 any ideas what could be wrong?
      • Mike James
        It's likely your PHP version is too old. Line 131 in TwitterOAuth.php is using an array shorthand introduced in php 5.4.
    • Vivek Moyal
      Here is the solution for more tweets at a time $count=10; $my_tweets = $connection->get('statuses/user_timeline', array('screen_name' => 'vivekmoyal', 'count' => $count)); for($i=0;$i text.""; } echo $alert;
    • Vivek Moyal
      Its worked for me but id i increase the count it wont work
    • Jedediah
      This is great! I have used WordPress plugins in the past to get tweets but they are always bloated with code and markup that I neither want nor need. This is perfect. Thanks a lot.
    • Mike
      This worked perfectly for me as well. Thank you for sharing!
    • Sandeep
      That's great Philipp. But how can we request other users tweet using php. Can you share with me?
      • Jedediah Sandeep
        I realize this is several months old, but in case it helps: somewhere around the middle of the tweets.php file you’ll see screen_name, and granted you’ve properly entered the keys to an app you’ve created, you can use anyone’s screen name, I know this because I couldn’t figure out why it kept showing the authors tweets instead of mine ;)