Join 3,496 readers in helping fund MetaFilter (Hide)

Tags:

Display random tweet using PHP?
July 10, 2012 4:52 PM   Subscribe

Dearest metafilter, show me the way to display a random tweet, selected by hashtag (preferable) or username, by using PHP/javascript/magic.

Basically, I'd like to build a very simple website that displays one random tweet, with a "click for another" link that refreshes the page and shows another tweet.

If it makes any difference I would own the twitter account and have access to the API, if it had to be done for a specific username.
posted by doublesix to Computers & Internet (2 answers total)
 
Tweet URLs aren't predictable, so you can't do this without a list of the Tweets. Either you can have something running periodically to archive the Tweets and store them on the server or you can fetch them in real time when the page loads. Then randomly pick one from the list as you would with anything else.

The first approach will make the page load faster and more reliably, and really is the only way to go if you want to randomly select from a large number of Tweets. If you only care about selecting from the last 5 or 10 or something, and don't care if it gets slow or breaks when Twitter is broken, you can use the second approach. But in that case you're probably better off doing the API request client-side in Javascript than doing it in PHP.
posted by primethyme at 5:25 PM on July 10, 2012


Take a look at the Twitter Streaming API. There's a very nice PHP library that makes it easy to subscribe to twitter streams. You'll need to register your application as a Twitter app and request stream access. Then twitter will send you an endless stream of tweets, either completely random or matching a search request (such as a hashtag). When someone hits the button on your web site, just grab the latest item from your stream, or pick one at random from the last 100, or whatever you like.

You can access their sample feed for free -- it gives you access to about 1% of all the tweets in the world, which is almost surely good enough for your purpose. Last time I tried it, I got 25-50 tweets per second when I wasn't running a search. When searching, you'll get whatever fraction of 25-50 tweets per second that actually matches the search.

You'd need to have access to a server where you're allowed to run a continuous process. Not all webhosts will do this. For example. at dreamhost, it's not allowed on a shared hosting plan, but it's legal on a virtual private server, which at the lowest level costs only $5/month on top of the shared hosting fees.

Alternately, you could run the stream on your own personal computer at home, and transmit randomly selected tweets to your webserver somehow. That has its own complications, though.

I posted an application that uses the streaming API on metafilter projects - you can find it from my profile.
posted by moonmilk at 8:07 PM on July 10, 2012 [1 favorite]


« Older Where does The Queen (of Engla...   |  A few recommendations needed f... Newer »
This thread is closed to new comments.