How do I post automatically from a PHP script to my Twitter account?
Archive - Originally posted on "The Horse's Mouth" - 2013-07-10 18:09:50 - Graham Ellis
How can I now post automatically from a PHP script to my Twitter account? It was "all change" earlier this year as the old API was deprecated then removed, to be replaced by 1.1. This can do a lot, but there's a fearsome list of steps according to some of the documentation, it's not easy to follow, and if you get authorisation codes wrong you'll find it simply doesn't work. Fortunately there are tools to help.
1. Sign up for a twitter account in the normal way if you don't already have the one you want to post to.
2. Sign up on the Twitter Developer's site for a new application, configure it, and use their tool to make an access token
Go to https://dev.twitter.com/apps
Sign in with the account from which you wish to post and create a new application.
For what we're doing, there is no need to add a Callback URL
The tool only does part of what you need; you should then go into the settings menu and change from "Read only" to "Read and Write". Other changes such as adding an Icon and the details of the technical author of the application are a good idea, but not mandatory.
From the details page of your application, select "create my access token.
You many need to refresh a couple of times, as the changes take a few seconds to appear - I suspect that the Twitter folks are clustering and that's the time taken for the whole cluster to be updated.
You will now have four impressively long key strings!
3. Get the helper library for your PHP
Go to https://github.com/abraham/twitteroauth , download the .zip, and copy it to and unpack it on your server. If you're new to Github, this may be yet another signup!
I have modified the .htaccess file on my server (in the downloaded directory) to deny access from all - thus to ensure that the files cannot be directly accessed. There are some tools in there, but the Twitter Developer's site in the previous step has done what you need and you only need to use one file in your program
If your PHP has been built without cURL, you'll need to rebuild or install that element. And you may also need to download and install the cURL libraries, and the ability to talk https outbound, on your server. Chances are you'll be OK, but the warned.
4. Try out a test tweeting PHP program
The source code of my test is [here] and you're welcome to try it out. You will need to provide the extra include file to load the keys, which Twitter instruct you to keep in separate places and encoded (yes, we can help with that on our various various PHP courses).
5. Implement code to change your message
It's not clever to try to repeat the same thing time and again to Twitter... so REMOVE my $say = 'write code here to read or generate the text you want to tweet';
and replace it with somethine useful
6. Either build your test code into your application at the point at which you want it to tweet, or set up a separate regular timed job through Crontab (that's what I have done) to take a look around from time to time and see if anything need tweeting.
I've used the technique described above to add a Twitter feed from the First Great Western Coffee Shop today, in response to user requests to provide them with updates / headlines via Twitter. The databases are queries every 30 minutes, and then any updates are posted; with our traffic levels, we're probably looking at posts in most hours.