Setting up automated tweets may at first sound like a huge task, but it really is simple. This blog post shows you how to set this up within 10 minutes using PHP.
- A web server with PHP support
- A Twitter account to tweet from
- Log into Twitter, then go to https://apps.twitter.com/
- Click on Create New App
- You’ll see the following screen. Fill in the Name, Description, and Website with anything you want.
- Note: in order to create an application, you must have a mobile phone number entered into your Twitter profile and verified
- Check the Developer Agreement box then click on “Create your Twitter Application”
- Your application will be created and you’ll be brought to the application configuration page:
- Click on the “Keys and Access Tokens” tab then at the bottom click on “Create my access token”. Keep this page open as we’ll be coming back to it in step #11.
- Download tmhOAuth: https://github.com/themattharris/tmhOAuth (click on “Clone or download” then “Download ZIP”)
- The two files you need are cacert.pem and tmhOAuth.php
- Using your favourite FTP program (or web interface), upload these two files onto your web server – optionally in a new folder (e.g. “catfacts” in this example):
- Time to get our hands dirty! Let’s code a very simple PHP script that will tweet via our Twitter account when executed.
- Create a new PHP file (named anything you want; in this example, “catfacts.php”), then open it in your favourite text editor.
- Paste in the following code:
<?php require 'tmhOAuth.php'; define("TWITTER_CONSUMER_KEY", "**Consumer Key (API Key) from step #6**"); define("TWITTER_CONSUMER_SECRET", "**Consumer Secret (API Secret) from step #6**"); define("OAUTH_TOKEN", "**Access Token from step #6**"); define("OAUTH_SECRET", "**Access Token Secret from step #6**"); $tmhOAuth = new tmhOAuth(array( 'consumer_key' => TWITTER_CONSUMER_KEY, 'consumer_secret' => TWITTER_CONSUMER_SECRET, 'token' => OAUTH_TOKEN, 'user_token' => OAUTH_TOKEN, 'secret' => OAUTH_SECRET, 'user_secret' => OAUTH_SECRET )); $code = $tmhOAuth->user_request(array( 'method' => 'POST', 'url' => $tmhOAuth->url("1.1/statuses/update"), 'params' => array( 'status' => 'A group of cats is called a clowder.' ) )); ?>
- Set the 4 “define” values with values from step #6.
- Save this PHP file and upload it to your webserver in the same folder as you uploaded the cacert.pem and tmhOAuth.php files in step #8.
- In your web browser visit the PHP file (e.g. http://www.server.com/catfacts.php)
- Go to your Twitter profile and you’ll see “A group of cats is called a clowder” has been automatically tweeted.
The above should help you get automated tweets up and running in a very short amount of time.
Some things you can do to take things to another level are:
- Creating a database/array of values and retrieving a random single item to tweet out (e.g. an array/database of cat facts)
- Creating a scheduled cron task to execute the above PHP file at a defined interval (e.g. daily at 12:00pm)
Aside from the example of daily cat facts, a major benefit of this is being able to send out automated tweets when there are changes in your web app.
Another application of this is coding a simple HTML form with a textarea which will tweet out the textarea value on submission. A use case for this is if you travel to a country which blocks access to Twitter.