Mobile Media

Shawn Van Every Shawn.Van.Every@nyu.edu

Week 5 - Leveraging Existing Services

Around the internet and world wide web there exist a variety of sites and applications that we might want to leverage for our own applications so as to not have to reinvent the wheel.

Fortunately, many of these sites and applications available as Web Services and have exposed their workings through APIs that we can use in our applications.

Email/MMS to Blog Post

Blogging software is often feature rich and gives us a nice easy to use interface for content management. I often think of blogs as a personal CMS (content management system).

One of the earliest features of most blog software is the inclusion of an interface called XML-RPC. XML-RPC allows programmatic access to an application through HTTP and XML.

Early on, a standard for blog software using XML-RPC was developed called the MetaWeblog API. This API defined the types of queries and structure of those queries for interacting with blog software.

Through this API we can make posts, edit posts, view content and so on.

Here is a snippet of PHP code which uses the PHP Pear package XML-RPC for posting to a blog:

Posting to blog with PHP
<?PHP
	// Couple of variables
	$username = "xxxx";
	$password = "xxxx";
	$xml_rpc_url = "/xxxx/xmlrpc.php";
	$blog_host = "www.xxxx.com";

	// Variables that need to be set
	$title = "test title";
	$description = "test description";
	$uploadfile_rel = "http://test.upload/something.3gp";

	// Construct the blog post
	$post_body = $description.'<br /><br />'."\n".'<a href="'.$uploadfile_rel.'">'.$uploadfile_rel.'</a>';

	// Include XML RPC Library
	require_once('XML/RPC.php');

	// Create the XML-RPC Structure	for the post
	$post_struct = new XML_RPC_Value(array(
		"title" => new XML_RPC_Value($title,'string'),
		"description" => new XML_RPC_value($post_body,'string')),
		"struct");

	// Create the XML-RPC structure for the username/password, including the post_struct
	$post_params = array(
		new XML_RPC_Value(1, 'int'),
		new XML_RPC_Value($username,'string'),
		new XML_RPC_Value($password,'string'),
		$post_struct,
		new XML_RPC_Value(true,'boolean')
	);

	// Using metaWeblog.newPost  create the msg
	$msg = new XML_RPC_Message('metaWeblog.newPost', $post_params);

	// Connect and send the message
	$cli = new XML_RPC_Client($xml_rpc_url, $blog_host);
	$resp = $cli->send($msg);
	
	// Check if the response exists
	if (!$resp)
	{
		// Couldn't connect, show the error
		echo("Communication error: " . $cli->errstr . "\n");
		exit;
	}
	else
	{
		$val = $resp->value();
		$data = XML_RPC_decode($val);
	
		// Check if there is a faultCode
		if (!$resp->faultCode())
		{		
			echo("POSTED " . $data . "\n");
		}
		else
		{
			echo("NOT POSTED " . $data . " " . $resp->faultCode() . " " . $resp->faultString() . "\n");
		}
	}
?>
		
This code snippet can be integrated into our "parseMailScript" script. Make sure you don't use variables of the same name (for instance "username" might already be used in the parseMailScript).

MMS/Email to Blip.tv

Blip.tv is an online video hosting service along the lines of YouTube. Fortunately for us, it is much more friendly to developers and the like giving us a nice API for working with it's system.

Using their API we can upload media to them for hosting, transcoding and distribution.

Here is a PHP code snippet which just like the above snippet can be dropped into the upload code or into the parseMailScript. It shows how you can submit media to blip.tv's system. From there, you can setup automatic transcoding to Flash and automatic cross-posting to other services (including your own blog).
<?PHP

//http://sourceforge.net/projects/snoopy/

//http://wiki.blip.tv/index.php/API_2.0:_Create_post_with_data

	include "Snoopy.class.php";

	$snoopy = new Snoopy;
	$snoopy->set_submit_multipart();


	$submit_url = "http://www.blip.tv/";

	$submit_files = array();
	$submit_files['file'] = '/path/to/file.3gp';
	
	$submit_vars = array();
	$submit_vars['title'] = "the title";
	$submit_vars['section'] = "file";
	$submit_vars['skin'] = "api";
	$submit_vars['cmd'] = "post";
	$submit_vars['post'] = 1;
	$submit_vars['description'] = "the description";
	$submit_vars['explicit'] = 0;
	$submit_vars['interactive_post'] = 0;
	$submit_vars['topics'] = 'xx,xxx,xxxx';
	$submit_vars['license'] = 1;
	$submit_vars['categories_id'] = -1;	
	
	$submit_vars['userlogin'] = 'xxx';
	$submit_vars['password'] = 'xxx';
		
	
	if ($snoopy->submit($submit_url,$submit_vars,$submit_files))
	{
		while(list($key,$val) = each($snoopy->headers))
			echo $key.": ".$val."<br>\n";
		echo "<p>\n";
		echo "<PRE>".$snoopy->results."</PRE>\n";
	}
	else
	{
		echo "error fetching document: ".$snoopy->error."\n";
	}
?>		
		

Download package

To Twitter

Twitter is a popular SMS/web presence application. It allows a social network to keep in touch with a light text only messaging interface.

Twitter has a nice API that we can publish status updates to. It might be a nice way to publicize that you have uploaded a new piece of media to your network. It might also be a way to create socially interactive media while on the go..

Here is a code snippet in the form of a web page:
<?PHP

	if (isset($_GET['thestatus']))
	{
		include "Snoopy.class.php";
	
		$snoopy = new Snoopy;
		$snoopy->set_submit_multipart();
	
		$username = "xxxx";
		$password = "xxxx";
		
		$snoopy->user = $username;
		$snoopy->pass = $password; 
		
		$submit_url = "http://twitter.com/statuses/update.xml";
	
		$submit_vars = array();
		$submit_vars['status'] = $_GET['thestatus'];
			
		
		if ($snoopy->submit($submit_url,$submit_vars))
		{
			while(list($key,$val) = each($snoopy->headers))
				echo $key.": ".$val."<br>\n";
			echo "<p>\n";
			echo "<PRE>".$snoopy->results."</PRE>\n";
		}
		else
		{
			echo "error fetching document: ".$snoopy->error."\n";
		}
	}
?>
<html>
	<head>
		<title>Twitter Poster Test</title>
	</head>
	<body>
		<form method="GET">
			<input type="text" name="thestatus" value="Say Something" />
			<input type="submit" name="submit" value="Submit" />
		</form>
	</body>
</html>
			


Download package