Category Archives: Flying Pie Notifier

Automatic Twitter Posting in PHP

I got a suggestion a few months ago to make FlyingPieNotifier.com post to Twitter. It is now working and you can see it posting every day at @FlyingPieNotify

Since the rest of the site was written in PHP already, I figured I would do the Twitter posting in PHP as well. I had done it before when working on webbr so I figured it would be just as easy.

But it turns out Twitter recently deprecated Basic authentication and made OAuth mandatory. This means I can’t just send my username and password, but instead needed to register as an application and go through a handshake process before I could post.

The good news is, if you are just posting from a single account and a single script, you only need to do this once.

Here is how I was able to get my server posting a name from the database to Twitter every day. For this, I assume you have a webserver running PHP that is accessible from the general internet (not on a local network only), and a basic knowledge of PHP.

Step 1: Register as an application

To use OAuth with Twitter, they require you to register as an application. This makes sense if you are writing an application to deal with multiple users, but even for 1 you have to do it.

If you go to http://twitter.com/apps you can register a new application. Most of the information here doesn’t matter for our purposes. The only thing you need to pay attention to specifically is the callback URL. For this, put a URL to a page that you can create on your server, such as http://example.com/twitter_callback.php. We will create this site soon.

Once your application is created, you will be given a consumer secret and a consumer key. We will need these in the next step.

Step 2: OAuth authentication

Now we need to go through the OAuth process to give our code access to the account. Since we only want to post for this one account, we don’t need to make this code very robust.

I used the Twitter-async library from https://github.com/jmathai/twitter-async. Grab the EpiCurl.php, EpiOAuth.php, and EpiTwitter.php files and put them on your server somewhere you can access them easily, preferably in your include_path.

Now create a file on your server that you can access. In this file, include the files we just created, and then add this code:


$twitterObj = new EpiTwitter($consumer_key, $consumer_secret);
echo 'Authorize with Twitter';

Make sure you replace the consumer key and secret with the ones you got when you registered your application.

Now create a second file with the name and URL that you put as the callback URL when you registered your application with Twitter. In this file, put:


$twitterObj = new EpiTwitter($consumer_key, $consumer_secret);

$twitterObj->setToken($_GET['oauth_token']);
$token = $twitterObj->getAccessToken();

echo 'oauth_token: '.$token->oauth_token.'
';
echo 'oauth_token_secret: '.$token->oauth_token_secret;

Again, make sure to include the Epi files and to put in your consumer key and secret.

Now load your first file in your browser, click the link, authorize your application with Twitter, and copy down the token and token secret that are shown in your browser.

Now we have authenticated, so we don’t need to do that again! You can just delete the 2 files you created, but leave the libraries there, we will use them again.

Step 3: Posting to Twitter

Now that we have an authentication token for our account and our application, we are ready to post.

To post, all we need to do is:


$twitterObj = new EpiTwitter($consumer_key, $consumer_secret, $oauth_token, $oauth_token_secret);

$result = $twitterObj->post_statusesUpdate(array('status' => $post));
$result = $result->responseText;

You need to make sure to have the keys from your application and the secret that you got when you authenticated here or it will not work.

Now if you look at this, you may be wondering about the last line. It looks like it is unnecessary, but because of the library we are using, we actually need it. This is because it is an asynchronous library, so our call to post does not mean that the post has been made. Referencing the response of our call forces our code to block until the call has completed.

Now you just need to write some code to generate your message and run when needed. For Flying Pie Notifier, I just used a cron job that runs once a day and pulls the name from the database for that day.

After this, there is a lot more you can do with these PHP scripts. The documentation for the Twitter-async library shows how to use the rest of the Twitter API in addition to posting.