How 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. (written 2009-03-28, updated 2010-06-26)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
H110 - PHP - HTML Web Page Data Handling [50] Current cost in your local currency - (2004-09-16)
[589] Robust PHP user inputs - (2006-02-03)
[789] Hot answers in PHP - (2006-07-02)
[896] PHP - good coding practise and sticky radio buttons - (2006-10-17)
[1001] .pdf files - upload via PHP, store in MySQL, retrieve - (2006-12-19)
[1053] Sorting people by name in PHP - (2007-01-26)
[1136] Buffering output - why it is done and issues raised in Tcl, Perl, Python and PHP - (2007-04-06)
[1169] Emailing as HTML (Web Page) - PHP example - (2007-04-30)
[1831] Text formating for HTML, with PHP - (2008-10-11)
[2025] Injection Attack if register_globals in on - PHP - (2009-02-04)
[2046] Finding variations on a surname - (2009-02-17)
[2135] What features does this visitors browser support? (PHP) - (2009-04-22)
[3036] Sending out an email containing HTML from within a PHP page - (2010-11-07)
[3926] Filtering PHP form inputs - three ways, but which should you use? - (2012-11-18)
G911 - Well House Consultants - Search Engine Optimisation [165] Implementing an effective site search engine - (2005-01-01)
[427] The Melksham train - a button is pushed - (2005-08-28)
[1015] Search engine placement - long term strategy and success - (2006-12-30)
[1029] Our search engine placement is dropping. - (2007-01-11)
[1344] Catching up on indexing our resources - (2007-09-10)
[1793] Which country does a search engine think you are located in? - (2008-09-11)
[1969] Search Engines. Getting the right pages seen. - (2009-01-01)
[1971] Telling Google which country your business trades in - (2009-01-02)
[1982] Cooking bodies and URLs - (2009-01-08)
[1984] Site24x7 prowls uninvited - (2009-01-10)
[2000] 2000th article - Remember the background and basics - (2009-01-18)
[2019] Baby Caleb and Fortune City in your web logs? - (2009-01-31)
[2045] Does robots.txt actually work? - (2009-02-16)
[2065] Static mirroring through HTTrack, wget and others - (2009-03-03)
[2106] Learning to Twitter / what is Twitter? - (2009-03-28)
[2137] Reaching the right people with your web site - (2009-04-23)
[2324] What search terms FAIL to bring visitors to our site, when they should? - (2009-08-05)
[2330] Update - Automatic feeds to Twitter - (2009-08-09)
[2428] Diluting History - (2009-09-27)
[2552] Web site traffic - real users, or just noise? - (2009-12-26)
[2562] Tuning the web site for sailing on through this year - (2010-01-03)
[2686] Freedom of Information - consideration for web site designers - (2010-03-20)
[2748] Monitoring the success and traffic of your web site - (2010-05-01)
[3670] Reading Google Analytics results, based on the relative populations of countries - (2012-03-24)
[3746] Google Analytics and the new UK Cookie law - (2012-06-02)
[4121] Has your Twitter feed stopped working? Switching to their new API - (2013-06-23)
Some other Articles
Business Networking for Melksham - 23rd AprilMySQL - looking for records in one table that do NOT correspond to records in another tableWhy most training fails ...Funny Amusing, Funny Amazing and The SmartsHow to tweet automatically from a blogHire Car, from Atlanta AirportRuby Programming and Rails - 4 different courses in oneAsk the Tutor - Open Source forumWhat do people think of our Apache httpd / Tomcat course?