ACCESS ALL AREAS

Auto-tweet a countdown to Twitter from your PHP website

2 December 2010 by Mike Gogulski
Posted in software | 4 Comments »

Recently I wanted to set up an automatic tweet counting down the days until the end of 2010 to remind people to send holiday cards to Bradley Manning and members of the US Congress. Here’s the solution I used:

  1. Log into your Twitter account.
  2. Go to http://api.twitter.com/apps..
  3. Register a new application
  4. When you’re done registering, go to http://dev.twitter.com/apps and click on the application name.
  5. Copy and save your consumer key and consumer secret.
  6. Click “My access token” at right.
  7. Copy and save your access token and your access token secret.
  8. Download Abraham Williams’ twitteroauth package from github. (If you have any trouble later, download and use the 0.2.0-beta3 branch instead of the latest.)
  9. Unpack the archive and transfer the directory it contains to your web server’s public_html or whatever directory is set up as your document root.
  10. IMPORTANT: Change the name of the directory to something secret. Make up a string of random letters and numbers. We’ll call this string SECRETDIR.
  11. Go into SECRETDIR, edit config.php and insert the consumer key, consumer secret, access token and access secret.
  12. Go into SECRETDIR and create a “tweet.php” file, like this:

    <?php

    require_once('config.php');
    require_once('twitteroauth/twitteroauth.php');
    $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
    $today = idate('z', time());
    $enddate = idate('z', strtotime('31 December 2010'));
    $daysleft = $enddate - $today;

    $tweetstr = 'Only ' . $daysleft . ' day';
    if ($daysleft > 1) $tweetstr .= 's';
    $tweetstr .= " left to send out a holiday card for Bradley Manning's freedom! http://bit.ly/dW5w5e #freebrad";

    $connection->post('statuses/update', array('status' => $tweetstr));

    ?>

  13. Now create a cron job on your server. I’m tweeting once daily at 9:00am in my webhost’s time zone, so I used:
    00 9 * * * wget http://www.bradleymanning.org/SECRETDIR/tweet.php
  14. Enjoy your auto-tweeting goodness!

Yes, using a SECRETDIR as a security (by obscurity) measure is not the best idea, but it does allow for a quick and dirty solution. Other solutions are possible, but this will get you started.

  1. 4 Responses to “Auto-tweet a countdown to Twitter from your PHP website”

  2. By Wayne on 16 August 2011

    Could I pay you to set up an auto tweet on my website?

  3. By Ene on 19 December 2013

    Can you help me to biud a countdown twitter bot? i’ve done what you say and i cant make it work.
    Thanks

  1. 2 Trackback(s)

  2. 17 December 2010: Why and How To Get Twitter Followers Quick & Easy – Tweet Adder | software for your business
  3. 26 December 2011: You know you’re a helplessly geek if you do what I just did XD « Q8GEEKS.ORG

    comments rss Comments RSS

Post a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Categories

  • Archives

  • Core Dogma