Many of our readers have been asking how they can access to twitter from PHP code and here we come with a simple tutorial that will give all the basics to do even more complex operations.
Let’s see in this simple tutorial how we can read our timeline and update our status from a simple piece of PHP code.
First of all we suggest you to have a look at the Twitter API wiki where you can have access to many useful information about the API and how to use them in different coding languages.
We will use for this example one of the many available open source libraries written to use twitter from PHP called PHP Twitter written by Tijs Verkoyen, you can download the library 1.0.2 version from here .
We will notice that PHP Twitter class has no real documentation and the only way to have some information is to read the Twitter Wiki API pages and understanding how API works just look into the PHP Twitter code for the corresponding function to call.
The first step to do is to include the PHP Twitter library into our code, so if we place the twitter.php file into the same directory of our project file we will write something like:
<?php include "twitter.php"; ?>
Now we need to create a Twitter class object and give it two parameters that are the twitter ID and password to log into twitter API with our identity.
<?php
include "twitter.php";
$twi_user = new Twitter("username","password");
?>
We are at this point authenticated into twitter api system so we can start with our requests, and the first thing we will do is to update our status using the updateStatus() function this way:
<?php
include "twitter.php";
$twi_user = new Twitter("username","password");
$user_text = "Hello this tweet is from PHP thx to @wpstudios
";
$twi_user->updateStatus($user_text);
?>
The content of the $user_text string should be written into your twitter status now, yes it’s just that simple
Another useful PHP Twitter library function is the getFriendsTimeline() that you can use to retrieve the 20 most recent statuses posted by you and your friends, actually this is like looking at the /home twitter page once logged into twitter.
This function returns an Array with all 20 statuses and additional information, we will use the print_r() function to read out the server response:
<?php
include "twitter.php";
$twi_user = new Twitter("username","password");
$aTimeline = $twi_user->getFriendsTimeline();
print_r($aTimeline);
?>
You can obviously go through the array and have a look for example at who was the last one to tweet and what he said:
<?php
include "twitter.php";
$twi_user = new Twitter("username","password");
$aTimeline = $twi_user->getFriendsTimeline();
echo $aTimeline[0][user][screen_name]." was the last to tweet and said : \n".$aTimeline[0][text]."\n\n";
?>
Note that this last example was intended to be used with commandline php, if you want to print the result on a web page just replace each ‘\n’ with a ‘<br />’.
Well this simple tutorial ends here and we hope to have given you some basic and useful ideas on how you can interact with twitter using some simple PHP code that you can use in your websites in different ways.





June 23rd, 2009 on 10:43
Nice! thx
July 10th, 2009 on 01:25
Please use proper code when writing an article that novice people will be utilizing.
$aTimeline[0]['user']['screen_name'];
String indexes need to be quoted just like any other time.
July 10th, 2009 on 07:53
Well actually you should but it works the same because PHP automatically converts a bare string (an unquoted string which does not correspond to any known symbol) into a string which contains the bare string.
It’s not my aim to teach PHP in this place but just give out an example on how you can do something so it’s up to single user to learn the coding language and it’s up to me to write pieces of code that just works as examples.
July 10th, 2009 on 15:15
There is a simple way to post to twitter – without using a Library.
July 10th, 2009 on 15:19
good trick even if you can only post with that, using Twitter PHP lib or directly curl functions you can do much more
July 17th, 2009 on 06:13
Thank you for this class. It’s very useful and easy to use. A good work!
August 1st, 2009 on 20:10
Nice Wrapper, using it for posting.
and yes there are simpler ways to post tweet with an api.
but a nice error info system always comes in handy.
grts,
@lisabell23
August 5th, 2009 on 05:45
Hi, I cant seem to get this to work.
Fatal error: Call to undefined function curl_init() in /home/bt/public_html/test/twitter/twitter.php on line 182
is the error I get… Any ideas?
here is the line from twitter.php
// init
$curl = curl_init();
August 5th, 2009 on 07:46
Most likely curl support is not enabled.
You can check by creating a .php file with a phpinfo(); command in it, like this:
< ?php
phpinfo();
?>
Browse to this and search/look for curl on the resulting page. If support is enabled, there will be a listing for it.
if you cannot find support for curl then there are many ways to enable it depending on the kind of apache/php install and hosting you are using, if you have shell or root access simply:
- Open apache\bin\php.ini
- Remove the semi-colon in front of this line: extension=php_curl.dll
- Restart Apache
Also have a look here:
http://it2.php.net/curl
August 15th, 2009 on 12:13
Parse error: syntax error, unexpected T_CONST, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or ‘}’ in /srv/xxxxxxxxxxxxxx/twitter/twitter.php on line 52
August 15th, 2009 on 14:34
seems like an error in the API class so you better ask the API coder at this address :
http://classes.verkoyen.eu/twitter/
also check out for new API version.
August 26th, 2009 on 19:19
Very nice tutorial – I had a test application up and running in ten minutes! Thank you!
September 19th, 2009 on 12:55
Scripts like these are good, but people can easily get hold of your Twitter password, even by using a Website Copier such as HTTrack.
I’ve made a simple script that puts your tweets on your page (user_timeline) WITHOUT using a password.
It simply reads an XML file.
See it here: http://bit.ly/QRMtJ
October 17th, 2009 on 10:55
Can’t seem to find any information on what is counted toward the query length in the Twitter API:
Usage Notes:
* Query strings should be URL encoded.
* Queries are limited 140 URL encoded characters.
And when you run the advanced search query tool:
http://search.twitter.com/advanced
It is not clear if names and operators are included in this length.
October 22nd, 2009 on 14:00
Thank you for sharing….
October 31st, 2009 on 09:31
the lib will return ??? if one tweet was written in unicode, such as chinese characters.
November 4th, 2009 on 18:07
great tutorial.
How do I add the time and source it was added?
anybody?
thanks
March 6th, 2010 on 03:33
WPStudios, I can understand how you would think that it’s not your aim to teach PHP but to showcase a nice neat feature. I get that, but you have to understand that especially with shared environments things like short tags, and ASP style tags may not be supported. When I wrote the documentation and extras section for http://theeasyapi.com I made it was setup in a way that all servers can use. That’s why I used XML instead of some other language to transport the data. I also used POST instead of SOAP because not every server can easily send a SOAP envelope. The Easy API is dedicated to providing API services mostly free of charge to developers from a wide range of languages.
I think this is a great article, and I really did enjoy reading it. I just think taking those few extra minutes making sure that it covers a wide range of PHP installations will do some good. It worked for me though right off the bat … haha.
Nice work,
Chad