Mobile Me(dia)

Shawn Van Every Shawn.Van.Every@nyu.edu
Spring 2009
H79.2690.1

Week 4 - 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.

Upload/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 easily integrated into our upload script from last week such as follows.

Integrating into upload script
<? echo "<?xml version=\"1.0\"?>\n"; ?>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<title>XHTML MP Test</title>
	</head>
	
	<body>
		<h1>File Upload Test</h1>
<?

	// Extra Database connect function
	$mySql = null;
	function sqlConnect() {
		# Configuration Variables
		$hostname = "localhost";
		$dbname = "xxxx";
		$username = "xxxx";
		$password = "xxxx";
		
		$mySql = mysql_connect($hostname, $username, $password) or die (mysql_error());
		mysql_select_db($dbname, $mySql) or die(mysql_error());
		
		return $mySql;
	}

	// Limit what people can upload for security reasons
	$allowed_mime_types = array("video/3gpp"=>"3gp", 
								"audio/x-wav"=>"wav", 
								"video/mp4"=>"mp4",
								"video/3gpp2"=>"3g2",
								"video/mpeg"=>"mpg",
								"video/quicktime"=>"mov",
								"video/x-quicktime"=>"mov",
								"video/x-msvideo"=>"avi",
								"image/jpg"=>"jpg",
								"image/jpeg"=>"jpg",
								"image/pjpeg"=>"jpg",
								"image/png"=>"png",
								"audio/vnd.wave"=>"wav"
								);

	// Make sure form was submitted
	if (isset($_POST['form_submitted']) && $_POST['form_submitted'] == "true")
	{
		// Check the mime type
		$allowed_types = array_keys($allowed_mime_types);
		$allowed = false;
		if (isset($_FILES['bytes']['type']))
		{		
			for ($i = 0; $i < sizeof($allowed_types) && !$allowed; $i++)
			{
				if (strstr($_FILES['bytes']['type'], $allowed_types[$i]))
				{
					$allowed = true;
				}
			}
		
			// If the mime type is good, save it..
			if ($allowed)
			{
				$uploadfilename = time() . "_" . rand(1000,9999) . "_" . basename($_FILES['bytes']['name']);
				// Make sure apache can write to this folder
				$uploaddir = '/home/sve204/public_html/mobilemedia/php_popper/posts/';
				$uploadfile = $uploaddir . $uploadfilename;
				$uploadrelativefile = 'http://itp.nyu.edu/~sve204/mobilemedia/php_popper/posts/' . $uploadfilename;
		
				if (move_uploaded_file($_FILES['bytes']['tmp_name'], $uploadfile))
				{
					// Make sure the file isn't executable and you can delete it if you need
					chmod($uploadfile, 0666);
					
					// Put it in the database
					/* Database Table:
						mobile_me_messages
						message_id int(11) auto_increment
						attachment varchar(255)
						subject varchar(255)
						body varchar(255)
						from_name varchar(255)
						from_domain varchar(255)					
					*/
					
					$subject = "";
					$message_text = "";
					
					if (isset($_POST['subject']))
					{
						$subject = $_POST['subject'];
					}
					
					if (isset($_POST['message_text']))
					{
						$message_text = $_POST['message_text'];					
					}
					
					##############
					# DO WHAT YOU WANT TO DO HERE
					##############
					// Post to Blog instead of do database insert
					
					// Couple of variables
					$username = "xxxx";
					$password = "xxxx";
					$xml_rpc_url = "/xxxx/xmlrpc.php";
					$blog_host = "www.xxxx.com";
				
					// Variables that need to be set
					// Using variables from upload form
					//$title = "test title";
					$title = $subject;
					//$description = "test description";
					$description = $message_text;
					//$uploadfile_rel = "http://test.upload/something.3gp";
					$uploadfile_rel = $uploadrelativefile;
				
					// 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");
						}
					}

					##############
					# FINISH 
					##############
					
					// Tell the user
					echo "<p>Success <br /> <a href=\"" . $uploadrelativefile  . "\">" . $uploadrelativefile . "</a></p>";
				}
				else
				{
					echo "<p>Error on upload...!</p>";
				}
			}
		}
	}
	else
	{
?>
		<form enctype="multipart/form-data" method="post" action="upload.php">
			<p>
				Title: <input type="text" name="subject" /><br />
				Description: <input type="text" name="message_text" /><br />
				<input type="file" name="bytes" />
				<input type="hidden" name="form_submitted" value="true" />
				<br />
				<input type="submit" name="submit" value="submit" />
			</p>
		</form>
		<hr />
		<p>
			Phone (iPhone) doesn't support file uploads?  <a href="mailto:test@mobvcasting.com">Email it instead</a>..
		</p>
<?
	}
?>
	</body>
</html>		
		
Try it: http://itp.nyu.edu/~sve204/mobilemedia/blogposting/upload.php or SMS 41411 keyword mome blog

Download Package: http://itp.nyu.edu/~sve204/mobilemedia/blogposting.zip

Of course, this could be integrated into the parseMailScript just as easily.

Upload/MMS 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

Upload/MMS 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:
<?PHP

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

//http://groups.google.com/group/twitter-development-talk/web/api-documentation

	include "Snoopy.class.php";

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

	$username = "xxx";
	$password = "xxx";
	
	$snoopy->user = $username;
	$snoopy->pass = $password; 
	
	$submit_url = "http://twitter.com/statuses/update.xml";

	$submit_vars = array();
	$submit_vars['status'] = "the status";
		
	
	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";
	}
?>
			


Download package