Passing Parameters in VoicePHP February 28, 2009
Passing parameters to an application is an important feature of any OS or programming language. It allows creating dynamic applications which can respond to the custom inputs. For example, in voice programming context, creating a common IVR which can serve to two different customers while maintaining the same code base. Again, this is where VoicePHP has an edge compared to VoiceXML (VoiceXML lacks such a basic feature).
Passing parameters to VoicePHP is a breeze. The best practice of accessing parameters in VoicePHP is using a tag. You can associate a tag to the application instance which will be accessible from VoicePHP script.
<?php
$parameter = $_VOICEPHP['calltag'];
// do something with the parameters
?>
Once you have the tag, you can use it to control the application flow OR reference it to fetch the other parameters from backend. More details here
Tag is not required to be unique & completely application dependet. Tag can be passed to VoicePHP from flash, click-2-call etc. using appropriate APIs.
Does'nt it look easy? Try VoicePHP for yourself and feel free to modify the above script. VoicePHP is easy and accessible from your free TringMe account. More details at http://voicephp.com/howitworks.html
Amazon Shopping – Just a Phone Call Away! February 9, 2009
How often you’ve had the need to check an item from an online vendor while shopping in a store? e.g. lets say you were in a bookshop and a novel looks interesting to you. Before purchasing it you want to go over reviews and maybe even want to do a comparative price search and possibly buy it online if it is priced better. In the ever connected world we live in, this isn’t anything new.
Being able to access information when I want it and where I want it is becoming more and more of a necessity than a luxury. Whatever ‘it’ is, the idea is to access all the information about that item at that particular moment.
In this application, we demonstrate how easy it is to use VoicePHP to build a complete voice accessible shopping experience. In particular, the application does the following:
- It allows users to type in an ISBN number. For e.g. (ISBN-10: 0375826726 or ISBN-13: 978-0375826726 for “Breaking Dawn”, ISBN-10: 1416562605 or ISBN-13: 978-1416562603 for “White Tiger”)
- Hear reviews, price and other information
- Provides skeleton code to implement the payment and shipping logic.
So next time you are in a bookstore, feel free to call 15672449576 to get more information on any book. This code can be easily extended to integrate other products offered by Amazonn or a completely different seller.
Disclaimer: Credit card number and shipping information is only being asked for demonstration purposes. We do not store any of that information and will not be sending that to Amazon (as you can see in the code). Please use your judgement when providing that information.
<?php
define('KEYID','*********');
define('AssocTag','******');
echo "Welcome to Voice PHP Demo of Amazon Book Look Up";
setvoiceoption("autospellnumbers",3);
setvoiceoption("interdigittimeout", 3000);
while(1) {
$isbn = prompt ("Please Enter the I S B N code of the book you would like to search. Press star key instead of X",1,30);
$len = strlen($isbn);
if((13 != $len) && (10 != $len)) {
echo "Invalid I S B N number";
continue;
}
elseif($len==10) {
$isbn = str_replace('*','x',$isbn);
}
break;
}
//Create request to amazon to search for book
$request = "http://ecs.amazonaws.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=".KEYID."&AssociateTag=".AssocTag."&Version=2006-09-11&Operation=ItemSearch&ResponseGroup=Medium,Offers&SearchIndex=Books&Keywords=".$isbn."&ItemPage=1";
$response = Fetch($request);
$parsed_xml = simplexml_load_string($response);
$numOfItems = $parsed_xml->Items->TotalResults;
$totalPages = $parsed_xml->Items->TotalPages;
if($numOfItems<0){
echo "No matches found for I S B N $isbn";
echo "Thank you for trying Voice PHP Demo";
return;
}
foreach($parsed_xml->Items->Item as $current) {
$offerlist = $current->Offers->Offer->OfferListing;
$content = $current->EditorialReviews->EditorialReview->Content;
if(isset($offerlist->OfferListingId)){
echo "Book Title : " . $current->ItemAttributes->Title;
}
if(isset($current->ItemAttributes->Author)) {
print("Author : ".$current->ItemAttributes->Author);
}
print("Amazon Offer Price: ".$offerlist->Price->FormattedPrice);
$content = strip_tags($content);
$len = strlen($content);
$read = prompt("Press 1 to hear the review or 2 to add to cart",1,5,"dtmf:1");
for($i=0;$i<$len && $read=="1";$i+=200) {
$review = substr($content,$i,200);
prompt($review, 1);
if($i==0) {
$read = prompt("Press 1 to hear more or 2 to add to cart",1,2,"dtmf:1");
}
}
if($read == "2") {
$cartId = add_cart($offerlist->OfferListingId);
}
}
if($cartId) {
$card = prompt("Enter your credit card number now",1,20);
//Perform the Credit Card check or Subscription processing etc...
echo "Credit card number entered is $card";
echo "Speak your postal address now, press hash when done.";
$address = record('#',5, 60);
echo "Your address has been recorded for shipping";
//Perform the shipping process...
}
echo "Thank you for trying out Voice PHP demo.";
function add_cart($offerListingId) {
$request="http://code.voicephp.com/samples/create_cart.php?Id=$offerListingId";
$response = Fetch($request);
if(empty($response)) {
echo "Could not add to cart";
return 0;
}
echo "Book added to cart";
$res = explode(' ',$response);
$cartId = $res[0];
return $cartId;
}
?>
Try It Now!
Click the button below or call +15672449576 to run the application.
Does'nt it look easy? Try VoicePHP for yourself and feel free to modify the above script. VoicePHP is easy and accessible from your free TringMe account. More details at http://voicephp.com/howitworks.html
Getting Past the VoiceXML Limitations – VoicePHP Audio Player February 3, 2009
One of the shortcomings of VoiceXML is its inability to do pause, resume, forward and rewind during a voice playback functionality. VoiceXML specification doesn’t even include such increasingly common features. Brian Brown has succintly identified the lack of same in a well-written article “Is VoiceXML the Right Tool for Your Voice Application”?
Lack of such functionalities really impact the usability of the applications. For example, lets say you are accessing your voicemails. The voicemail is being played is a long one and you probably didn’t hear the previous sentense very well and want to rewind a bit, you are stuck now. There is no way this can built in VoiceXML due to lack of state information about voice stream being played. Surprising? yeah, but true!
Contrast that with the ability of VoicePHP and see for yourself how easy it is to build a audio playback application. Potential applications that can benefit from this functionality are an email reader, voicemail player, a music on demand media player etc.
This application demonstrates:
- How to use DTMF to aid user to navigate between typical media player functions.
- Usage of Play to control Play, Rewind, Forward functionality.
<?php
echo "Welcome to Voice PHP audio player";
echo "Audio playback will begin shortly. At anytime, press 2 to pause,
3 to stop,
4 to rewind,
5 to fast forward,
6 to restart and
0 to exit.";
$play = 1;
$offset=5000; // Rewind and Fast-Forward Offset
$streampos = 0;
while($play) {
$choice = play("http://code.voicephp.com/samples/3732.pcm",true,$streampos,1);
$streampos = $_VOICEPHP['streampos'];
$streamlen = $_VOICEPHP['streamlen'];
switch($choice) {
case 0: // Exit
exit();
case 4: // Rewind
$streampos = ($streampos<$offset)?0:($streampos-$offset);
break;
case 5: // Forward
$streampos = ($streampos>($streamlen-$offset))?0:($streampos+$offset);
break;
case 2: // Pause
if(prompt("Playback paused. Press 1 to resume, any key to exit",1,10,"dtmf:1")!="1") {
$play=0;
continue;
}
break;
case "": // Playback Completed.
case 3: // Stop
if(prompt("Playback paused. Press 1 to resume, any key to exit",1,10,"dtmf:1")!="1") {
$play=0;
continue;
}
case 6: // Restart
$streampos = 0;
break;
}
}
?>
In part two of this application, we will demonstrate a fully functional media player application. We will show also share with you a re-usable mediaplayer function which implements the common features of a mediaplayer.
Try It Now!
Click the button below or call +15672445604 to run the application.
Does'nt it look easy? Try VoicePHP for yourself and feel free to modify the above script. VoicePHP is easy and accessible from your free TringMe account. More details at http://voicephp.com/howitworks.html
Listen to your Emails – Part I January 28, 2009
Continuing with the series of integrating voice into the web, here is an IMAP email client application. This code shows how VoicePHP can be used to easily write web applications which speaks and listens.
This demo does the following:
- It establishes an IMAP connection to the specified IMAP server.
- It fetches the new messages in the last 2 days.
- It reads out the subject of the mail and allows you to hear the body of the message by pressing 1.
The application can be easily customized to your needs. For sake of demonstrating it, we’ve customized it to read emails from test.voicephp@gmail.com. To try it out, just send an email to test.voicephp@gmail.com and then click on the widget below to hear your message being read out.
We can’t wait to see how a VoiceXML based email client code would look like. Any takers?
<?php
echo "Welcome To VoicePHP E Mail Reader Demo";
$mail = imap_open('{imap.gmail.com:993/imap/ssl}INBOX', 'test.voicephp@gmail.com', '********')
or die("Connection to mail server failed: ". imap_last_error());
//Search mailbox for unread messages
$messages=imap_search ($mail, 'UNSEEN SINCE ' . date('d-M-Y', time() - 7200) . '');
if($messages > 1) {
rsort($messages);
$count = count($messages);
echo "You have $count new unread mails";
echo "To hear the body of a mail, press 1 after the subject is read,
press 0 to exit anytime";
get_subject($messages,$count,$subject,$header,$mail);
for($i=0;$i<$count;$i++){
$voicetype = ($i&1)?"female":"male";
setvoiceoption("voicetype", $voicetype);
$op = prompt($subject[$i], 1, 1, "dtmf:1");
if($op == "1") {
read_mail($messages[$i], $mail);
echo "Returning to subject menu";
}
elseif($op == "0") {
break;
}
}
}
else {
echo "You have no new mails";
}
imap_close($mail);
echo "Thank you for trying Voice PHP Demo";
//Get the subject of the mail ..
function get_subject($messages,$count,&$subject,&$header,$mail){
for($i=0;$i<$count;$i++) {
//Get IMAP header info for mail
$header = imap_headerinfo($mail, $messages[$i]);
$subject[$i] = "Mail from " . $header->from[0]->personal . " with subject " . $header->subject;
}
}
//Read a mail body..
function read_mail($num,$mail) {
// Get the message body
$st = imap_fetchstructure($mail, $num);
if (!empty($st->parts)) {
for ($i = 0, $j = count($st->parts); $i < $j; $i++) {
$part = $st->parts[$i];
if ($part->subtype == 'PLAIN') {
$body = imap_fetchbody($mail, $num, $i+1);
}
}
} else {
$body = imap_body($mail, $num);
}
echo $body;
}
?>
Try It Now!
Click the button below or call +15672440715 to run the application.
Does'nt it look easy? Try VoicePHP for yourself and feel free to modify the above script. VoicePHP is easy and accessible from your free TringMe account. More details at http://voicephp.com/howitworks.html
Back-end driven VoicePHP application January 27, 2009
All practical applications need to use the data on the back-end to drive certain aspect of the application behavior. For voice based interaction, it can be quite easily done in VoicePHP.
Diagram below shows a typical application flow.
Key steps to look at:
- User interacts with the VoicePHP script.
- VoicePHP script interacts with the back-end**.
- Back-end does a database lookup and formulates appropriate response which is then conveyed to user via VoicePHP interaction.
Consider the sample code below. It is split into a front-end VoicePHP file that interacts with the user and a back-end PHP file (back-end could be anything – PHP, ASP, .NET etc.).
<?php
// frontend.voicephp
// ...
$pin = prompt("Enter Your Pin Number"); // Back-end can use
$result = notify("http://backend.myserver.com/authenticate.php?pin=$pin");
echo "You name is $result";
// ...
?>
<?php
// backend.php
//PIN used to validate the user.
$pin = $_REQUEST['pin'];
// ... assuming database is connected ..
$result = mysql_query("select * from user_info where userpin = '$pin'");
$row = mysql_fetch_row($result);
// ... do other processing and return ...
// $row[1] -> User Name
echo "$row[1]";
?>
As you can see, in the front-end VoicePHP file,
- The user is asked to enter the PIN number.
- Using notify, the application passes the information to the back-end script.
- It uses the response from notify to echo the output to user.
In the back-end script,
- The PIN number is used to do a database lookup.
- Relevant information (name, in this case) is returned back.
** Note: If one was using the “premise model”, the VoicePHP script could interact with the database directly.
Does'nt it look easy? Try VoicePHP for yourself and feel free to modify the above script. VoicePHP is easy and accessible from your free TringMe account. More details at http://voicephp.com/howitworks.html
Get your Caller Id January 24, 2009
In this example, we show you how easy it is to retrieve caller-id using VoicePHP. Being able to use caller-id can open doors to interesting usage for identifying the user and customizing the application content/behavior.
<?php
echo "Welcome to VoicePHP Caller ID Demo";
setvoiceoption("autospellnumbers", 7);
echo "Your caller ID is {$_VOICEPHP['callerid']}";
?>
Try It Now!
Call +15672449992 to run the application.
Does'nt it look easy? Try VoicePHP for yourself and feel free to modify the above script. VoicePHP is easy and accessible from your free TringMe account. More details at http://voicephp.com/howitworks.html
Voice Twitter – Access your tweets over phone – Part I January 21, 2009
Do you Twitter? Would’t it be convenient to hear and reply back to tweets using your phone? Here is the code that makes it all possible.
In this code sample, we show you an easy way to retrieve twitter feeds and play them using VoicePHP. This code example also shows how to use JSON (JavaScript Object Notation) within VoicePHP.
<?php
// The twitter feed you are interested in
$tweetname = 'CNN';
$tweeturl = 'http://twitter.com/statuses/user_timeline/cnn.json';
echo " Welcome To The Voice PHP Tweet Reader ";
$response = Fetch($tweeturl); //Get all the tweets
if (empty($response)) {
echo 'No Tweets';
return;
}
echo "Reading $tweetname Tweets";
$json_res = json_decode ($response, true);
$i = 0;
$number_msg = count($json_res);
while($i < $number_msg){
$text = $json_res[$i]['text'];
//Eat any URL we encounter in the tweet
$result = preg_replace("^((http://)([a-zA-Z0-9?&%.;:/=+_-]*))^",'', $text);
$i++;
speak("Reading Tweet $i ",0);
speak($result,1);
sleep(2);
}
echo "Thank you for trying Voice PHP Tweet Reader";
?>
In part 2 of this, we will also show how easy it is to Tweet back using voice.
Try It Now!
Click the button below or call +15672449921 to run the application.
Does'nt it look easy? Try VoicePHP for yourself and feel free to modify the above script. VoicePHP is easy and accessible from your free TringMe account. More details at http://voicephp.com/howitworks.html
Voice RSS – Let your blog speak – Part I January 19, 2009
How often have you thought that it would be a cool idea to hear your favorite RSS feeds? If you are a blogger or a webmaster, wouldn’t it be a nice addition to have your blogs be spoken to the reader?
Well, think no more. Code below shows a simple implementation of an Voice RSS Reader.
Here are the key steps of this application:
- Specify the URL for the RSS feeds you are interested in.
- Ask the user to select a feed by pressing the appropriate keys on the keypad
- Fetch the RSS feeds using a helper function
- Speak the title of the feeds and if user is interested, the description of that feed
Does'nt it look easy? Try VoicePHP for yourself and feel free to modify the above script. VoicePHP is easy and accessible from your free TringMe account. More details at http://voicephp.com/howitworks.html
Create your first IVR application using VoicePHP January 18, 2009
In this example, we will show you how easy it is to implement an IVR application using VoicePHP in merely a few lines of code. The code is commented and self explanatory. Leave us a comment if you have any questions. Read more
Does'nt it look easy? Try VoicePHP for yourself and feel free to modify the above script. VoicePHP is easy and accessible from your free TringMe account. More details at http://voicephp.com/howitworks.html
Hello world! – First VoicePHP Application January 15, 2009
Welcome to VoicePHP!!!
In the tradition of demonstrating a “hello world” application, this script shows how to speak hello world using VoicePHP. Simple, eh? Read more
Does'nt it look easy? Try VoicePHP for yourself and feel free to modify the above script. VoicePHP is easy and accessible from your free TringMe account. More details at http://voicephp.com/howitworks.html






