Updating Twitter from Mathematica
Since Twitter has become a way to instantly collaborate it seemed logical to implement a way to update your Twitter status from Mathematica. The method I created uses Web Services Link to access a WSDL based SOAP server automatically generated by NuSOAP.
First, download the latest version of NuSOAP and put it in a folder on your server. Let’s say this folder is called ‘twitter’ and is located in the root of your server.
Now we need to create the php file that will generate the WSDL service. Let’s call this file mathematica.php. Much of this code was adapted by Scott Nichol’s tutorial on Programming with NuSOAP using WSDL.
< ?php
require_once('lib/nusoap.php');
$server = new soap_server();
$server->configureWSDL('twitterupdate', 'urn:twitterupdate');
$server->register('Twitter',
array('name' => 'xsd:string'),
array('return' => 'xsd:string'),
'urn:twitterupdate',
'urn:twitterupdate#Twitter',
'rpc',
'encoded',
'Provides the Twitter function to change your Twitter status using Mathematica'
);
function Twitter($status) {
if strlen($status) < = 140 {
$user = 'yourtwitterusername';
$pw = 'yourtwitterpassword';
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, "http://twitter.com/statuses/update.xml");
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "status=$status");
curl_setopt($curl_handle, CURLOPT_USERPWD, "$user:$pw");
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
} else {
return "Your update must be 140 characters or less";
}
}
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
?>
Now to access the WSDL file generated by mathematica.php we can go to http://www.yourserver.com/twitter/mathematica.php?wsdl. This WSDL service provides the operation named Twitter which will update the status for the username and password provided in the php file. The Update operation takes a string as its only argument.
Now to access the Twitter operation in Mathematica we must first load the WebServices package and then install the service provided by automatically generated WSDL file.
In[1]:= <<WebServices`
In[2]:= InstallService["http://www.yourserver.com/twitter/mathematica.php?wsdl"]
Out[2]:= {Twitter}
In[3]:= Twitter["I updated my Twitter status using Mathematica: http://tinyurl.com/mm2twitter"]
In the future I plan on creating a persistent session so you can use the InstallService to input your Twitter username and password. I would also like to support TinyURL generation.






[...] from Mathematica Here is a combination I never thought I would see – using Mathematica to update your twitter account courtesy of code from James Rohal. I’ve not tried it yet but it looks like [...]
April 6th, 2009 at 4:57 pmI have a package that calls the Twitter API directly from Mathematica.
http://dev.ragfield.com/2009/04/twittering-with-mathematica.html
-Rob
April 16th, 2009 at 11:29 pmThat is a very good idea! It could be used if you are running a long calculation that takes weeks but every now and then it gives you a point/result that you could publish real time!
July 4th, 2009 at 7:02 pmamazing. can we do it from matlab, too? also, i want your blog software! i’ve been wanting to do a math blog for a while now, but never really figured out how to get
going in there.
July 16th, 2009 at 2:34 pm