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.