SMS

SMS or Short Message Service is a protocol for sending short messages from mobile phones to other mobile phones. Text messaging or texting is a very popular activity worldwide, coming in only second in usage to voice calling. Approximately 50% of users worldwide have used SMS.

Due to the popularity of SMS and people's comfort with it and the fact that it enjoys a very high level of interoperability between carriers, it has been used in a variety of contexts beyond individuals messaging each-other.

FROM MOBILE HEALTHCARE TO PAYMENTS TO MICROFINANCE, SMS REMAINS 'TIP OF THE SPEAR'

Interactive Television
Daily messaging services
Microblogging
Donations/Premium SMS
More about SMS: Wikipedia: Short message service

Textmarks - Receiving SMS

TextMarks is a free online service that allows for the sending and recieving of text messages on the regular mobile network. TextMarks allows anyone to register a keyword of their choosing and to utilize the shortcode 41411.

(I registered the mobme for demonstration purposes)

One of the great things about TextMarks is that you can have the service hit a script running on a remote server through a web request (HTTP).

Using this functionality as well as the ability to send SMS back you can allow mobile users to participate in many different types of interactive applications.

To have my keyword hit a script that I am running on a webserver, I have to choose "Respond to a keyword with text from a webpage" when setting up the keyword with TextMarks:



The URL I put in is as follows: http://itp.nyu.edu/~sve204/mobilemedia_spring10/mobme.php?phone=\p&message=\0

TextMarks substitutes the user's phone number where the "\p" is and the message they send where the "\0" is. (More documentation can be found here: http://www.textmarks.com/dev/docs/recv/.

Now we can write the "mobme.php" script..
<?
        // This PHP file: textmarks.php gets hit with phone and message when someone texts 41411 mobme

        // Phone Number:
        $phone = $_GET['phone'];

        // Message:
        $message = $_GET['message'];

        // Save the message to the text file which changes my pages
        $fp=fopen('/home/sve204/public_html/mobilemedia_spring10/mobme/mobme.txt','w');
        fwrite($fp,$message);
        fclose($fp);

        echo "Yay!!!  Thanks";
?>
		
Now when ever someone texts 41411 with the keyword "mobme" this script is called and they get a text back that says: "Yay!! Thanks"

In addition this script writes out the contents of their message to a text file: "mobme.txt" on the server.

There are several other services aside from Textmarks that you may want to investigate (use instead). They typically work in a similar manner:

Mozes
Gumband

PHP 101

PHP is a very useful language for doing many types of development. Primarily it is used for web development (hence the name, Hypertext Preprocessor) but it is certainly not limited to that. For our purposes we will be using PHP as both a web development tool and as a means to glue together various webservices that we might access through our mobile applications (which is what we are doing with Textmarks).

Basic HTML

<html> <!-- Required for all HTML pages -->
	<head> 
		<title>The Top Bar Title</title>
	</head>
	<body>
		Some information
		<!-- A Comment Tag -->
	</body>
</html> <!-- Closing tag, required -->
		
Example 1

Basic HTML with PHP
PHP = Hypertext Preprocessor

<html> <!-- Required for all HTML pages -->
        <head>
                <title>The Top Bar Title</title>
        </head>
        <body>
                Some information
		<br> <!-- A line break -->
                <!-- A Comment Tag -->
		<?  // Denotes the start of PHP processing
        		echo "The Date and Time is: ";  // print a string, end with a semicolon
        		$mydata = "July 29, 2004"; // Assign a variable 
        		echo $mydata; // print the string variable
		?> 

        </body>
</html> <!-- Closing tag, required -->
		
Example 2
Take note of the different comment styles.
The "//" style within PHP and "<--" outside of PHP (normal HTML)
Variables begin with "$".
"=" are assignment.
"echo" is the print function.
";" ends line of code.
PHP files are saved with ".php" not ".html"

Creating a PHP file for use on the command line

PHP can be developed in any text editor and doesn't require any compilation. More Information:
  • PHP: Basic Syntax - Manual


  • Variables

    Variables in PHP are not strictly typed, meaning that you do not have to differentiate between strings, integers and so on. They start with a "$".

    		<?PHP
    			$myString = "hello world\n";
    			echo($myString);
    			$myInteger = 1003;
    			echo($myInteger . "\n");
    			$somethingelse = false;
    			echo($somethingelse . "\n");
    		?>
    		
    More Information:
  • PHP: Types - Manual
  • PHP: Variables - Manual
  • PHP: Constants - Manual


  • Mathematical Operators

    		<?PHP
    			$aValue = 0;
    			$aValue++;
    			echo("aValue now = " . $aValue . "\n");
    			$aValue = $aValue + $aValue;
    			echo("aValue now = " . $aValue . "\n");
    			// % + - * / ++ -- and so on, same as in Processing/Java
    		?>
    		
    More Information:
  • PHP: Expressions - Manual
  • PHP: Operators - Manual


  • Control Structures, Logical Operators and Loops

    		<?PHP
    		
    			// If Statement
    			$aValue = 0;
    			if ($aValue == 0)
    			{
    				echo("aValue is 0");
    			}
    			else if ($aValue == 1)
    			{
    				echo("aValue is 1");
    			}
    			else if ($aValue >= 2)
    			{
    				echo("aValue is greater than or equal to 2");
    			}
    			else
    			{
    				echo("aValue something else");
    			}
    			echo("\n");
    			// Other Logical Operators ==, >, <, >=, <=, ||, &&
    
    		
    			// For loop
    			for ($i = 0; $i < 10; $i++)
    			{
    				echo("\$i = $i\n");
    			}
    			
    			// Also While
    		?>
    		
    More Information:
  • PHP: Control Structures - Manual


  • Arrays and Loops

    		<?PHP
    			// Pretty normal array
    			$anArray = array();
    			$anArray[0] = "Something";
    			$anArray[1] = "Something Else";
    			for ($i = 0; $i < sizeof($anArray); $i++)
    			{
    				echo($anArray[$i] . "\n");
    			}
    			
    			// Key Value Map
    			$anotherA = array("somekey" => "somevalue", "someotherkey" => "someothervalue");
    			$keys = array_keys($anotherA);
    			$values = array_values($anotherA);
    			for ($i = 0; $i < sizeof($keys); $i++)
    			{
    				echo($keys[$i] . " = " . $values[$i] . "\n");
    			}
    		?>
    		
    More Information:
  • PHP: Arrays - Manual


  • Functions

    		<?PHP
    			
    			function myFunction($somearg)
    			{
    				// You would do something here
    				return "You passed in $somearg";
    			}
    			
    			$passing_in = "Hello World";
    			$return_value = myFunction($passing_in);
    			echo($return_value);
    			
    			echo("\n");
    		?>
    		
    More Information:
  • PHP: Functions - Manual


  • Classes and Objects

    		<?PHP
    			class MyClass
    			{
    				var $myClassVar;
    
    				function set_var($new_var)
    				{
    						$this->myClassVar = $new_var;
    				}
    
    				function get_var()
    				{
    						return $this->myClassVar;
    				}
    			}
    	
    			$aClass = new MyClass;
    			$aClass->set_var("something");
    			echo("Var: " . $aClass->get_var() . "\n");		
    		?>
    		
    Classes and Objects in PHP are very similar to Java/Processing. Syntactically though they don't use the "." operator, instead using "->". Also, in the class definition you need to use the "this" keyword.

    More Information:
  • PHP: Classes and Objects - Manual


  • Some Interesting Functions

    isset()
    		<?PHP
    			$somevar = NULL;
    			if (isset($somevar))
    			{
    				echo("somevar is set");	
    			}
    			else
    			{
    				echo("somevar is NOT set");
    			}
    		?>
    		
    mail
    			$mailmsg = "A Mail Message";
    			$mailsubject = "A Mail Subject";
    			$mailfrom = "vanevery@walking-productions.com";
    			$mailto =  "vanevery@walking-productions.com;
    			$mailheaders = 'From: ' . $mailfrom . "\n"; 
    			$mailheaders .= 'Reply-To: ' . $mailfrom . "\n"; 
    			$mailheaders .= 'Return-Path: ' . $mailfrom . "\n";
    			$mailheaders .= "Message-ID: <".time().">\n"; 
    			$mailheaders .= "X-Mailer: Shawns PHP Mailer Function\n";          
    			ini_set('sendmail_from',$mailfrom);
    			$return = mail($mailto, $mailsubject, $mailmsg, $mailheaders);
    			ini_restore('sendmail_from'); 
    			echo "Mail Sent?: ";
    			var_dump($return);		
    		
    More information:
  • PHP: Mail - Manual


  • More Information:
  • Tons More Here: PHP: Function Reference - Manual
  • Textmarks Send SMS

    Another great thing that can be done with TextMarks is to send an SMS to an individual. (First the individual needs to subscribe to your keyword).

    In order to do this, you need to register for an API key from TextMarks (it takes some time so do it early): http://www.textmarks.com/dev/api/reg/?ref=devsb

    TextMarks, being the nice folks that they are have written a PHP library that uses their API, making things like sending messages very easy.

    Documentation is here: http://www.textmarks.com/dev/docs/apiclient/php/

    Their PHP library is here: http://www.textmarks.com/dev/docs/apiclient/php/TextMarksAPIClient.class.php Here is an example PHP script which uses their library and API: (PHP Page: sendmesomething.php)
    <?php
    // Require/Import the TextMarksAPIClient.class.php file
    // Download from: http://www.textmarks.com/dev/docs/apiclient/php/TextMarksAPIClient.class.php
    require "TextMarksAPIClient.class.php";
    
    ini_set('display_errors', true);
    ini_set('display_startup_errors', true);
    error_reporting(E_ALL);
    
    // This page get's called by AJAX when someone is on a page for a while (2 minutes)...
    // Now I know I have audience and I can go online and interact with them..
    // I should probably check the user agent to make sure it is a real person and not Google or Yahoo but they shouldn't run JavaScript anyway..
    sendTextToMe();
    
    // Send a text message to a single TextMark subscriber:
    function sendTextToMe()
    {
    	
    	// TextMarks API Key: Request from: http://www.textmarks.com/dev/api/reg/?ref=devapi
    	$sMyApiKey='MYAPIKEY';
    	
    	// TextMarks Username or Phone Number
    	$sMyTextMarksUser = 'MYPHONENUMBER'; //(or my TextMarks phone)
    	$sMyTextMarksPass = 'MYPASSWORD';
    	
    	// TextMarks Keyword
    	$sKeyword = 'MYKEYWORD';
    	
    	// Who are you going to send the text to?
    	$sTo = 'MYPHONENUMBER';
    	
    	// The message to send
    	$sMessage = "Someone is on your site";
    	
    	// Create the TextMarks Object with the above parameters
    	$tmapi = new TextMarksAPIClient_Messaging($sMyApiKey, $sMyTextMarksUser, $sMyTextMarksPass);
    	
    	// Send the message!
    	$tmapi->sendText($sKeyword, $sTo, $sMessage);
    	
    	// For debugging, dump out the results
    	var_dump($tmapi);
    	
    	echo "Notified";
    }
    ?>