How to tweet automatically from a blog
Archive - Originally posted on "The Horse's Mouth" - 2009-03-28 18:02:12 - Graham EllisHow to Tweet from a Blog
I'm using the MoveableType Blog software for "The Horses's Mouth" ... and I want to tweet my subject line for each new post. But I'm lazy, so I have set it up to happen automatically.
1. I post my blog entry in the usual way
2. I have added the following script at /home/wellho/tweeter/blogtweet on my server:
<php
mysql_connect("127.0.0.1","databaseuser","databasepassword");
mysql_select_db("wellho");
$rs = mysql_query("select entry_id,entry_title from mt_entry order by entry_id desc limit 1");
$hv = mysql_fetch_assoc($rs);
$leid = file_get_contents("blogtweet.txt");
if ($leid == $hv[entry_id]) exit();
`curl --basic --user wellho:twitterpassword --data status="[diary] - $hv[entry_title] - http://www.wellho.net/mouth/$hv[entry_id]_.html" http://twitter.com/statuses/update.xml`;
$fh = fopen("blogtweet.txt","w");
fputs($fh,$hv[entry_id]);
fclose($fh);
?>
See update (at end of this post) w.r.t. this code, which needed update, August 2009
3. I have added a regular timed (crontab) job on the server as follows:
10,25,45,58 * * * * cd /home/wellho/tweeter; /usr/local/bin/php blogtweet > /dev/null
So - about every 15 to 20 minutes - my server checks if a new blog entry has been added. And if it has, it uses curl to submit an update to twitter.
Note:
a) If I submit two blog items very quickly in succession, the first one may not be tweeted.
b) There is a time lapse of up to 20 minutes between me blogging and tweeting.
c) My script isn't 'clever' if I include quotes in my subject line
d) I haven't (in this simple example) checked the blog entry to make sure that it's actually been published.
e) An alternative curl that I tried (and worked) using Json was as follows:
curl -u wellho:twitterpassword -d status="Trying to Autotweet" http://twitter.com/statuses/update.json
Update - 9th August 2009 - The --basic option no longer works - indeed, tweets using it are simply being lost. Remove the option and carry on as before. I suspect that this change relates to the problems that Twitter have had over the last few days.