Redial: Interactive Telephony : Week 5

Programming Asterisk: More AGI, More PHP and perhaps more more

More PHP and AGI

Variable Scope

One thing that we neglected to cover last week was the scope of variables in PHP.

Consider the following PHP:

		<?PHP
			function myFunction()
			{
				$bVariable = "world";
			}
			
			$aVariable = "hello";
			
			echo($aVariable . " " . $bVariable);
		?>
		
It will only print "hello" and will not show "hello world" as we are intending. This is an example of variable scope, since the $bVariable is defined within myFunction() it is not accessible from outside the function. We could remedy this by "returning" $bVariable and assigning or echoing it out from there.

		<?PHP
			function myFunction()
			{
				$bVariable = "world";
				return $bVariable;
			}
			
			$aVariable = "hello";
			$cVariable = myFunction();
			
			echo($aVariable . " " . $cVariable);
		?>
		
Another thing that should be known is that "global" variables, those defined outside of functions, are not automatically accessible inside functions and have to be declared first.

This won't work as intended.

		<?PHP
			function myFunction()
			{
				$bVariable = "world";
				echo($aVariable . " " . $bVariable);
			}
			
			$aVariable = "hello";

			myFunction();
		?>
		


By declaring the "global" variable inside the function we can use it. This will work as intended.

		<?PHP
			function myFunction()
			{
				GLOBAL $aVariable;
				$bVariable = "world";
				echo($aVariable . " " . $bVariable);
			}

			$aVariable = "hello";
			
			myFunction();
		?>
		


Yet another way of doing the same thing would be to pass in the variable that you want access to.
		<?PHP
			function myFunction($cVariable)
			{
				$bVariable = "world";
				echo($cVariable . " " . $bVariable);
			}

			$aVariable = "hello";
			
			myFunction($aVariable);
		?>		
		
More Information:
  • PHP: Functions - Manual
  • PHP for Web Development

    Of course, PHP is very good for web development.
    		<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
    PHP can occur anywhere in the page within "<? ?>" tags
    		<?
    		function printDate()
    		{
    			echo "The Date and Time is: ";
    			$mydata = "July 29, 2004";
    				echo $mydata;
    		}
    		?>
    		<html>
    				<head> 
    						<title>The Top Bar Title</title>
    				</head>
    				<body>
    						<? 
    					printDate();
    						?>
    				</body>
    		</html>
    		
    Example
    PHP with Forms PHP is ideal for generating HTML forms and even more so for dealing with data that comes back from forms.

    A Basic HTML Form

    <html>
            <head>
                    <title>A Basic HTML Form</title>
            </head>
            <body>
    		<form action="form_example.php" type="get">
    			First Name:<input type="text" name="first_name"><br>
    			Last Name:
    			<select name="last_name">
    				<option value="Smith">Smith</option>
    				<option value="Doe">Doe</option>
    			</select>
    			<br>
    			<input type="submit" value="Submit Me">
    		</form>
            </body>
    </html>
                    
    HTML Form Example
    Form Processing with PHP Dealing with data submitted via a form
    <?
    		/* 
    		Description: Helper function to get values from a form post (type="post" or query string (type="get")
    		Returns: value of key or null on failure
    		*/
    		function getPOSTorGETValue($key)
    		{
    			if (isset($_POST[$key]))
    			{
    				$value = $_POST[$key];
    			}
    			else if (isset($_GET[$key]))
    			{
    				$value = $_GET[$key];
    			}
    			else
    			{           
    				$value = null;
    			}    
    			return $value;
    		}
    ?>
    <html>
    		<head>
    				<title>The Form Output</title>
    		</head>
    		<body>
    				<?
    					$first_name = getPOSTorGETValue("first_name");
    					$last_name = getPOSTorGETValue("last_name");	
    					echo "Your First Name is: " . $first_name . "<br>";
    					echo "Your Last Name is: " . $last_name . "<br>";
    				?>
    		</body>
    </html>
    		
    Example Form Output

    Database Interaction

    PHP has a pretty straight forward method to working with MySQL databases. I am not going to go into SQL which is the language used to put data in and get data out of a database so if you would like to learn it this provides a good starting point: http://www.webdevelopersnotes.com/tutorials/sql/

    There is one caveat to working with Asterisk on the asterisk server and MySQL on ITP's webserver, the port to connect to MySQL on ITP's webserver is not open. If you plan to connect with an AGI script or PHP script from the Asterisk server, you can request that Nancy open the port for your user from the Asterisk server.

    // Some Variables used for connecting
    $hostname = "itp.nyu.edu"; // Hostname of the MySQL server
    $dbname = "sve204"; // Database name, this is your net-id if using ITP's server
    $username = "sve204"; // MySQL username, this is probably your net-id
    $password = "xxxxxx"; // MySQL password, this is NOT your net-id password			
    
    // Create a connection to the database server
    $mySql = mysql_connect($hostname, $username, $password) or die (mysql_error());
    
    // Tell mysql that you want to use your database
    mysql_select_db($dbname, $mySql) or die(mysql_error());
    
    // A string for our SQL statement
    $query = "insert into callers (caller_id) values ('17188096659')";
    
    // Execute the query, actually inserts the data
    $result = mysql_query($query, $mySql);		
    		
    The above inserts one row into the database table callers and sets the caller_id equal to 17188096659.

    Here is an example that pulls all of the caller_id values out of the table
    // Here is a query function that I like to use to make my life easier
    function sqlQuery($query)
    {
    	global $mySql;
    	$data = null;
    	$result = mysql_query($query, $mySql);
    
    			# This set's up an associative array (key->value pair) for all of the data returned
    			if (sizeof($result) > 0)
    			{
    					$num_fields = mysql_num_fields($result);
    					$row_cnt = 0;
    					while ($row_data = mysql_fetch_array($result)) {
    							for ($cnt = 0; $cnt < $num_fields; $cnt++) {
    									$field_name = mysql_field_name($result, $cnt);
    									$data[$row_cnt][$field_name] = $row_data[$cnt];
    							}
    							$row_cnt++;
    					}
    			}
    	return $data;
    }			
    
    			
    // Some Variables used for connecting
    $hostname = "itp.nyu.edu"; // Hostname of the MySQL server
    $dbname = "sve204"; // Database name, this is your net-id if using ITP's server
    $username = "sve204"; // MySQL username, this is probably your net-id
    $password = "xxxxxx"; // MySQL password, this is NOT your net-id password			
    
    // Create a connection to the database server
    $mySql = mysql_connect($hostname, $username, $password) or die (mysql_error());
    
    // Tell mysql that you want to use your database
    mysql_select_db($dbname, $mySql) or die(mysql_error());
    
    // A string for our SQL statement
    $query = "select caller_id from callers";
    
    // Execute the query, actually inserts the data
    //$result = mysql_query($query, $mySql);	
    // Using my function above
    $result = sqlQuery($query);
    
    for ($i = 0; $i < sizeof($result); $i++)
    {
    	echo("Row: " . $i . " value: " . $result[$i]['caller_id'] . "\n");
    }
    		

    More AGI

    Predefined Variables
    			// Predefined AGI Variables, send them to the Asterisk console for debugging
    			$agi->conlog($agi->request["agi_request"]);
    			$agi->conlog($agi->request["agi_channel"]);
    			$agi->conlog($agi->request["agi_language"]);
    			$agi->conlog($agi->request["agi_uniqueid"]);
    			$agi->conlog($agi->request["agi_callerid"]);
    			$agi->conlog($agi->request["agi_dnid"]);
    			$agi->conlog($agi->request["agi_rdnis"]);
    			$agi->conlog($agi->request["agi_context"]);
    			$agi->conlog($agi->request["agi_extension"]);
    			$agi->conlog($agi->request["agi_priority"]);
    			$agi->conlog($agi->request["agi_enhanced"]);
    			$agi->conlog($agi->request["agi_accountcode"]);
    			$agi->conlog($agi->request["agi_network"]);
    			$agi->conlog($agi->request["agi_network_script"]);		
    		
    A Full Example AGI Portion
    Web Portion

    Midterm Thoughts

    Voice 2.0
    Telephony Examples
    Nerd Vitties
    Get Your Email By Telephone: Introducing MailCall for Asterisk
    NewsClips for Asterisk
    Wakeup System
    Weather System
    As Easy As 1-2-3: A Telephone Reminder System for Asterisk