Lunch Dictator or Tweeting from PHP

A while back I needed a way to simply post tweets from PHP. To my disappointment most examples I found were about reading tweets or required a bunch of PHP modules I did not have the opportunity to use on the server where the script was going to run. So I had to do some work.

But first back to why I needed this. Many years ago me and some friends had the problem that it took us forever to decide on where to go to lunch every day since each day everybody had different preferences. In order to save time I created the Lunch Dictator. The original lunch dictator published a time and place each day for us to have lunch. Funny enough a few months ago my current team were experiencing the same problem so I decided to revive the lunch dictator. But this time as a twitter account.

Since I have a webhost that allows me to run scheduled jobs on a server with PHP I wanted to create the tweets form PHP but as mentioned above most examples only covers reading the twitter feed and the few examples of actually tweeting from PHP that I found were either using old APIs or PHP modules my webhost did not have (and I couldn't install). I guess that is the usual curse of open source that I seem to always encounter. So by digging through the twitter documentation I came up with the following code in order to tweet from PHP. If you decide to use it you need to create your own twitter application and fill in the right keys and secrets.

No comments:

Post a Comment