Connecting To Twitter API Using PHP

/ PHP / by Paul Robinson / 7 Comments
This post was published back on February 23, 2010 and may be outdated. Please use caution when following older tutorials or using older code. After reading be sure to check for newer procedures or updates to code.


Twitter no longer allow unauthenticated connections to their API. As such this tutorial will not work. To connect to Twitter I would advise using a oAuth library such as the one created by Matt Harris. I will try to make a tutorial about how to use it very soon.

Adding Twitter to your website is something that quite a lot of people do, and why not? One of the most common reasons is that users of your site can follow you via Twitter, and then they’l have somewhere to go (and you have somewhere to broadcast updates) should your site be down for whatever reason.

Important Notes

First there are some important things to remember about using Twitter’s API. First is that there is a limit to how many times you can request information from the API within a hour. This limit is 150 requests & it resets every hour. The easiest way to get around the limit is to cache the information in some way, generally to a file.

Making A Connection

I am going to use cURL to connect to Twitter. I have found that it is the most reliable & arguably the fastest method of retrieving API data from Twitter. You could just a easily use file_get_contents() as long as your server has URL file access enabled on fopen, but I’m going to stick with cURL.

Our connection looks like this:

This creates a connection to Twitter and places all the data retrieved into the $content variable. Just remember you need to change twitterusername to your Twitter username. Also you can change the number after count to get more (up to 200) or less tweets.

Decode The JSON

The data will be in json format, although you can get the data in XML or RSS formats too. If you do not have PHP5 you will not be able to decode json using json_decode(), you could try using the JSON class found here. To use it, save the file as a PHP file (something like json.class.php) then use something like the following:

If you have PHP5 then all you need to do is this:

Output The Data

Now we have decoded the information into an object/array we can output the data. Here is an example of some code you could use, as it all depends on what data you want to output from the Tweets:

That would output the status text of each tweet in paragraphs. There is a lot of other data held along with it, I can’t list it all as it would be quite a large list. If you want to see what data you can do this:

This will list out a nicely formatted list of all the data held in $content. Then you just have to decide what to do with the data, but that I leave to you.

Final Notes

There are a few last things to note about using the Twitter API. First is that I wouldn’t start playing around with the Twitter API until you have a decent knowledge of PHP & have learned at least a little about Object Oriented Programming. Second is that the URL I’ve used has retweets removed from it by Twitter. Third is that due to retweets being removed there is a quirk where the amount of tweets you request is not correct. If you request the 10 latest Tweets and 3 of them were retweets you will only get 7 back. Other than merging retweets back onto your tweets (which is complicated & requires authentication) there doesn’t seem to be anything that can be done about it. One thing I do want to say is that this is my experience, I could be wrong but so far I can’t find any other reason for the missing tweets I’ve experienced.

I think that’s about it. If you have any questions, or need any help let me know in the comments. If you think a downloadable example would help, let me know & I’ll see if I can knock one up. 😉


Author’s gravatar

Good stuff here in this post Paul. Gonna need to keep this post handy for a website I’m currently working on.

Author’s gravatar

Thanks Tyler, let me know if theres anything else I can help with. 😉

I’ve just noticed that when someone replies (here in the comments) the form fields are too big and overflow. I guess that’s what you get if you don’t check your theme as a guest. 😆 *facepalm* I’ll have to fix that.

Author’s gravatar

Hi, Paul! How are you?

Your post is very simple to understand, and for me this is perfect. Congratulations!

So, I’m starting a working final project do university and I need to create a integration interface entre a vTiger CRM and the Twitter Search API. I’m a little lost but maybe you can help me with some ideas.

The purpose is connect from the vTiger CRM to Twitter Search API and to find based on key-words setup in CRM the referentes (mentions) of products. So… simplify, the first step that I need is to creat scripts.php to make tests, searching, like the button Search on the Twitter home page.

I hope that you can understand me.

Author’s gravatar author

Hi Tiago,

I have zero experience with vTiger so I don’t think I’ll be much help. As for the Twitter end, you will need to query the search API instead of the API I do. You can find the information about the correct URL here.

I’ve never used that part of the API, but other than the fact you can only get information from it in JSON or ATOM format, it should be pretty much the same.

I hope that helps a little.

Author’s gravatar

Thanks for the very useful article! I never realised that the JSON decode function in PHP5 was so simple to use… I am so following your RSS feed now 😀

Author’s gravatar author

No problem. Thanks for following my RSS feed.

Just to let you know. Posts are a little sparse at the minute as we are just finishing our new theme. Thing should be back to normal by the end of the week at the latest. 😉

Author’s gravatar

Appreciate the straight forwardness, Its a nice change then most tutorials, sounding like lectures 😛

Older Comments
Newer Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

I'll keep your WordPress site up-to-date and working to its best.

Find out more