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.

Permalink · Written on: 04-05-09 · 5 Comments »

5 Responses to “Updating Twitter from Mathematica”

  1. Walking Randomly » Tweeting from Mathematica wrote:

    [...] 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 pm
  2. Rob Raguet-Schofield Identicon Icon Rob Raguet-Schofield wrote:

    I 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 pm
  3. AskAPhD.org Identicon Icon AskAPhD.org wrote:

    That 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 pm
  4. adam Identicon Icon adam wrote:

    amazing. 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 \LaTeX going in there.

    July 16th, 2009 at 2:34 pm
  5. xeyztsrm Identicon Icon xeyztsrm wrote:

    MOHtn4 vmjzuqbanxdn, [url=http://ytfzwhcjayia.com/]ytfzwhcjayia[/url], [link=http://tatiemtlfxmz.com/]tatiemtlfxmz[/link], http://vandgvounptk.com/

    June 20th, 2010 at 6:16 am

Leave a Reply

This blog has LaTeX enabled. Use $$stuff$$ for inline code and $$!stuff$$ for math mode.