tags:

views:

829

answers:

6

A client would like me to add their Twitter stream to their website homepage, using a custom solution built in PHP.

The Twitter API obviously has a limited number of calls you can make to it per hour, so I can't automatically ping Twitter every time someone refreshes my client's homepage.

The client's website is purely HTML at the moment and so there is no database available. My solution must therefore only require PHP and the local file system (e.g. saving a local XML file with some data in it).

So, given this limited criteria, what's the best way for me to access the Twitter API - via PHP - without hitting my API call limit within a few minutes?

A: 
  1. a cron job (not likley - if there's not even a database, then there are no cron jobs)
  2. write the microtime() to a file. on a page view compare the current timestamp to the saved one. its the difference greater than N minutes, pull the new tweetfeed and write the current timestamp to the file
  3. if the front page is a static html-file not calling any php, include an image <img src="scheduler.php"/> that returns an 1px transparent gif (at least you did it this way when i was young) and does your twitter-pulling silently

or do you mean local-local filesystem, as in "my/the customers computer not the server"-local?

in this case:

  1. get some server with a cron job or scheduler and PHP
  2. write a script that reads and saves the feed to a file
  3. write the file to the customers server using FTP
  4. display the feed using javascript (yes, ajax also works with static files as datasources). jquery or some lib is great for this
  5. or: create the tweet-displaying html file locally and upload it (but be careful ... because you may overwrite updates on the server)

imo: for small sites you often just don't need a fully grown sql database anyway. filesystems are great. a combination of scandir, preg_match and carefully chosen file names are often good enough.
and you can actually do a lot of front-end processing (like displaying XML) using beautiful javascript.

Schnalle
A: 

Since we don't know your server config I suggest you set up a cron job (assuming your on a Linux box). If you have something like cPanel on a shared hosting environment than it should be not much of an issue. You need to write a script that is called by cron and that will get the latest tweets and write them to a file (xml?). You can schedule cron to run every 30 min. or what ever you want.

Luke
A: 

You may want to use TweetPHP by Tim Davies. http://lab.lostpixel.net/classes/twitter/ - This class has lots of features including the one you want, showing your clients time line. The page shows good examples on how to use it.

You can then put the output of this in a file or database. If you want the site visitor to update the database or the file like every 5 minutes so, you can set a session variable holding a timestamp and just allow another update if the timestamp was at least 5 minutes ago.

Hope this helps

+2  A: 

It will be quite easy, once you can pull down a timeline and display it, to then add some file-based-caching to it.

check age of cache
Is it more than 5 mins old?
    fetch the latest information
    regenerate the HTML for output
    save the finished HTML to disk
display the cached pre-prepared HTML

PEAR's Cache_Lite will do all you need on the caching layer.

Alister Bulman
A: 

My suggestion: Create a small simple object to hold the cache date and an array of tweets. Every time someone visits the page, it performs the following logic:

A) Does file exist?

Yes: Read it into a variable No: Proceed to step D)

B) Unserialize the variable (The PHP pair serialize()/unserialize() would do just fine)

C) Compare the age of the cache stored with the current time (a Unix timestamp would do it) Its over 5 minutes from each other:

D) Get the newest tweets from Twitter, update the object, serialize it and write in the cache again. Store the newest tweets for printing, too. Its not: Just read the tweets from the cache.

E) Print the tweets

Simplest and easiest way to serialize the object is the serialize()/unserialize() pair. If you're not willing to put off the effort to make the object, you could just use 2D array, serialize() will work just fine. Give a look on http://php.net/serialize

Considering you have no cPanel access, its the best solution since you won't have access to PEAR packages, cron or any other simpler solutions.

DfKimera
+1  A: 
array(
'lastrequest' => 123,
'tweets' => array ()
)

now in your code put a check to see if the timestamp in the datastore for lastrequest is more than X seconds old. If it is then its time to update your data.

serialize and store the array in a file, pretty simple

Chad Scira