TwitterPost

Summary: easily post updates to your Twitter account via PHP functions
Version: 2009-08-03
Prerequisites:
Status: not currently working, as of Summer 2010 - twitter has switched to OAuth - recipe to hopefully be updated soon
Maintainer: overtones99

Questions answered by this recipe

  • How can I send status updates to Twitter directly from my wiki?

Description

easily post updates to your Twitter account via PHP functions.

Notes

This code allows an admin to easily post updates to a Twitter account. Simply call the following function with the appropriate username, password, and a message:

	twitterPostMsg_func($twitter_username, $twitter_password, $twitterMsg);

... and that's it - your status update is sent.

I am currently using this function in coordination with a cron job that calls the code daily, and sends tweets listing any newly created pages. (By updating at the end of the day, as opposed to immediately after a page has been posted, visitors are less likely to be directed to a page still-under-construction, and allows the editor to set up PTV's to be included in the tweet, i.e. {$:Description}, etc)

This recipe was developed using code posted on Gareth Rushgrove's website, http://morethanseven.net/2007/01/20/posting-to-twitter-using-php/.

Note that there are apparently many more Twitter API libraries out there that allow all sort of interaction with Twitter, but this was the simplest, most straightforward code for simply sending updates that I found while sleuthing around. Feel free to explore those as well, and feel free to improve this code, update this page, etc...

Installation

Simply download twitterposts.phpΔ, and add the following lines to your config.php:

   
   ## POST TO TWITTER
   # Set username and password
   $twitter_username = 'yourUsername';
   $twitter_password = 'yourPassword';
   # turns on "URL shortening" 
via bit.ly - 
   # see the related
Bit.Ly Cookbook Recipe
   #$twitterShortUrls = true;
   $twitterPosts_verbose = true;  # outputs twitter messages to the screen before being sent
   # Load Basic Twitter-Posting Code
   include_once("$FarmD/cookbook/twitterposts.php"); 

Suggested Implementation

The basic call, from config.php or any other PHP function you devise, is simply:

twitterPostMsg_func($twitter_username, $twitter_password, $twitterMsg, $verbose);
  • $twitter_username, $twitter_password are self-explanatory
  • $twitterMsg is your 140-character status update that you'd like to post on your Twitter profile - it's simply a "string"
  • $verbose is optional, and if set to true will print a success or failure message.

There are likely several ways this function can be used - perhaps messages can be sent via a public or private comment box? The way I've been using it is to schedule a daily cron job to send updates to Twitter, listing any new pages that have been created in the last 24 hours; the cron job simply calls http://mysite.com/Main/Index?action=twitterpost. Here is an example of the PHP code I've been using to do this.

  • twitterMsgPostEXAMPLE.phpΔ - gathers pages created and/or updated in the last 24 hours, and calls the twitterPostMsg_func for each of them...
  • cron job command -
    • daily, every night at midnight: 0 0 * * * wget http://mySite.com/Main/Index?action=twitterpost
    • just for testing, every minute: * * * * * wget http://mySite.com/Main/Index?action=twitterpost

In addition, Twitter'ers are used to using and seeing shortened URL's. If you're also fond of these, then you might want to enable the Short Urls recipe, and set $twitterShortUrls = true;. Without it, your longer URL's will often appear abbreviated with "..." appended to the end.

Release notes

  • 2009-08-03: fixed potential bug - setting verbose to 'false' would have prevented function from returning 'true' or 'false'
  • 2009-07-22: Added to PmWiki Cookbook...

See also

Contributors

  • overtones99 (feel free to edit this recipe. i'm not always available to maintain this sort of thing, so feel free to dive in and amend this...)

Comments

This space is for User-contributed commentary and notes. Please include your name and a date (eg 2007-05-19) along with your comment. Optional alternative: create a new page with a name like "ThisRecipe-Talk" (e.g. PmCalendar-Talk).

User notes? : If you use, used or reviewed this recipe, you can add your name. These statistics appear in the Cookbook listings and will help newcomers browsing through the wiki.