[GUIDE] Setting up Automated Tweets in 10 Minutes

Monday, February 27, 2017 @ 1:43 pm

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.

Prerequisites:

  1. A web server with PHP support
  2. A Twitter account to tweet from

Steps:

  1. Log into Twitter, then go to https://apps.twitter.com/
  2. Click on Create New App
  3. You’ll see the following screen. Fill in the Name, Description, and Website with anything you want.

    1. Note: in order to create an application, you must have a mobile phone number entered into your Twitter profile and verified
  4. Check the Developer Agreement box then click on “Create your Twitter Application”
  5. Your application will be created and you’ll be brought to the application configuration page:
  6. 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.
  7. Download tmhOAuth: https://github.com/themattharris/tmhOAuth (click on “Clone or download” then “Download ZIP”)
    1. The two files you need are cacert.pem and tmhOAuth.php
  8. 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):
  9. Time to get our hands dirty! Let’s code a very simple PHP script that will tweet via our Twitter account when executed.
  10. Create a new PHP file (named anything you want; in this example, “catfacts.php”), then open it in your favourite text editor.
  11. 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.'
    	)
    ));
    ?>
    1. Set the 4 “define” values with values from step #6.
  12. 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.
  13. In your web browser visit the PHP file (e.g. http://www.server.com/catfacts.php)
  14. Go to your Twitter profile and you’ll see “A group of cats is called a clowder” has been automatically tweeted.

Next Steps:

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.