Displaying tweets on my site or locally

Displaying her tweets on a HTML or blog is very simple with PHP script. And the script actually allows scripts to display any Twitter account on a page to be informed of last messages.

In a second part, we'll see how to display tweets on a site with JavaScript, without limitations due to shared hosting and therefore a shared IP - because Twitter limits the number of queries per IP.

This version allow to create a page of tweets from several account on an HTML page, like the demo included in the archive, and place the page in the www directory of Wamp for a local display.

- PHP 5.
- The open source library Snoopy (included in the archive).
- A personal IP, because a shared IP could exceed the authorized access limits.
- For local use, install Wamp (no limits).

Update 2013: The API has evolved and an authentication number is now required. The code below no longer works.

How it works

The tweets of an account are available on the Twitter site at the following URL:


$username is a PHP variable that contains the account name. For example: scriptol for the account of this site.

You can access the xxxx.json file through HTTP with a function of the Snoopy library or equivalent. This file contains a table holding various information for each tweet. Its general structure is as follows

Array  ( 
     [0] => Array  
      [user] => Array  
         [followers_count] => ...

Entry [0] is the first tweet. There are so many entries that tweet. It contains several arrays or strings.
[text] is the index of the text of a tweet.
[user] [followers_count] contains the number of followers.

The script can afford to see the whole data. To do this, you must uncomment the line:

// print_r( $twitter);

and look at the source code of the page for a better formatting with indentation.

The script and its usage

It retrieves the table on the Twitter site and loops through the tweets.

To avoid spamming Twitter, we do one query per hour, and the tweets are saved in a file. There is also a limit of 150 requests per hour from Twitter.

To use the script:

  1. Assign the name of the account to $username.
  2. Assign the number of tweets to display to $maxtweets.
  3. Place the script in an HTML page.
  4. Upload it or store it in the www directory of Wamp.

and the rest is automatic ... Verify that your hosting is configured for PHP 5 or more.

In the case of online use, the message "Rate limit exceeded. Customers may not make more than 150 requests per hour" can occur when too many requests from the same IP have done, and that can easily happens with a shared hosting (see Rate Limiting).

The archive contains the library snoopy.php and tweets.php a demonstration HTML page that works online or locally.

Download the archive