CallXML 2.0 Development GuideHome  |  Frameset Home

  Introduction to Server Side Languages  |  TOC  |  tutorial Hello world with ColdFusion  
This documentation is for CallXML 2, which has been superceded by CallXML 3. The CallXML 2 language is not being updated any longer. CallXML 3, however, has many new features and is actively being enhanced. If you're writing a new CallXML application, you should use CallXML 3. Click here for the CallXML 3.0 documentation.

Tutorial: Hello World with PHP


This Lessonl will step you through the creation and deployment of a "hello world" application using a server-side scripting language such as active server pages, ColdFusion, PHP, perl, etc. This Lesson is based on the things you accomplished in the previous tutorials. If you haven't yet done those Lessons, you'll need to go through them first. This Lesson assumes that you have a working knowledge of at least one of these languages and that you have access to a web server that understands your language of choice.

Note: Be aware that the Voxeo webhosting service does not have provisions for any file extensions other than .xml, .gsl, and .grammar files. Attempting to run any of the following tutorials when the files live at webhosting.voxeo.net simply won't work. If you do wish to run these tutorials, then you will need to find a host that supports the appropriate markup language. If you do not have a webserver in-house to accomadate this, then a simple Google search for '(PHP/JSP/whatever) hosting' should turn up dozens of possible candidates for you to mull over.


In this Lesson, we will:

Step 1: Reviewing the callxml we want to create

We will use a server-side scripting language to dynamically create the callxml used in the scripts "helloworld-pound.xml" and "helloworld-star.xml" from previous Lessons. Those files contained the callxml:

"HelloWorld-pound.xml"

<?xml version="1.0" encoding="UTF-8" ?>
<callxml version="2.0">

  <block label="pound">
    <text>you pressed the pound key.</text>
  </block>

</callxml>


"HelloWorld-star.xml"

<?xml version="1.0" encoding="UTF-8" ?>
<callxml version="2.0">

  <block label="star">
    <text>you pressed the star key.</text>
  </block>

</callxml>


Step 2: Modifying our callxml to use a server-side scripting language


We will need to make some minor modifications to the helloworld.xml script, which looked like this:


<?xml version="1.0" encoding="UTF-8" ?>
<callxml version="2.0">

  <block>
    <playAudio value="helloworld.wav"
              termdigits="*#"/>

    <ontermdigit value="*">
      <goto value="helloworld-star.xml"
            submit="*"
            method="get"/>
    </ontermdigit>

    <ontermdigit value="#">
      <goto value="helloworld-pound.xml"
            submit="*"
            method="get"/>
    </ontermdigit>

  </block>

</callxml>


We will need to modify our goto elements to link to our server-side scripting language, the changes are in bold below. For this tutorial we will be using PHP v4.0 so we will need to change the scipt to read:


<?xml version="1.0" encoding="UTF-8" ?>
<callxml version="2.0">

  <block>
    <playAudio value="helloworld.wav"
              termdigits="*#"/>

    <ontermdigit value="*">
      <goto value="helloworld-handler.php?key=star" submit="*" method="get"/>
    </ontermdigit>

    <ontermdigit value="#">
      <goto value="helloworld-handler.php?key=pound" submit="*" method="get"/>
    </ontermdigit>

  </block>

</callxml>


Notice that we are no longer linking to a callxml file, instead we are linking to a .php file. Also notice that we are linking to the same file in both cases and passing along a variable in a name/value pair that will let our next script know what key was pressed.


Step 3: creating a server-side script to produce callxml

Now we need to create our server-side PHP v4.0 script. We will examine this script in small chunks and discuss some of the new things that we can do by using a server-side scripting language. It is important to note that this is not intended to be an introduction to PHP. For additional information on PHP please visit PHP.net.


<?PHP


This indicates the start of a block of PHP code, and ?> indicates the end of the block.


  $s = $HTTP_GET_VARS["key"];


This line uses the PHP environment variable: $HTTP_GET_VARS and examines the value that we passed into this script. It assigns that value to our variable "s". (Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive).


  echo "<?xml version="\1.0\" encoding=\"UTF-8\" ?> ";
  echo "<callxml version=\"2.0\"> ";


These lines generate the standard first two lines of a callxml script. Note that echo is not actually a function (it is a language construct) so you are not required to use parantheses with it, but you will need to include the quotation marks.


  echo "  <block label=\" $s \"> ";


This line begins our block and uses our "s" variable as the label name.


  if ($s == "star") {
    echo "    <text>you pressed the star key.</text> ";
  } else {
    echo "    <text>you pressed the pound key.</text> ";
  }


These lines implement a conditional statement that examines our "s" variable. If "s" is the word "star" then we produce callxml to indicate that the star key was pressed, if not then we know that the pound key was pressed and we generate the appropriate callxml.


  echo "  </block> ";
  echo "</callxml>";


These lines generate callxml that close our block and callxml script.


?>


This line indicates that we have finished our PHP script.


The resulting file should read:

<?PHP

  $s = $HTTP_GET_VARS["key"];

  echo "<?xml version=\"1.0\" encoding=\"UTF-8\" ?> ";
  echo "<callxml version=\"2.0\"> ";
  echo "  <block label=\" $s \"> ";

  if ($s == "star") {
    echo "    <text>you pressed the star key.</text> ";
  } else {
    echo "    <text>you pressed the pound key.</text> ";
  }

  echo "  </block> ";
  echo "</callxml>";

?>


Now all that we need to do is save this file as "helloworld-handler.php" in the same location as our "helloworld.xml" script.


Step 4: Upload, and try it out

Your updated hello world Callxml application is now done. Call the number associated with your callxml application, and you'll find that the results of this Lesson work just like Lesson 5, except this time we used a server-side script.


  CallXML 2.0 source code.


What we covered





  ANNOTATIONS: EXISTING POSTS
vsefcik
3/27/2006 6:10 PM (EST)
The above sample won't work because the first 3 echo statements enclose the XML in quotation marks.  If the lines are changed from:
  echo "<?xml version="1.0" encoding="UTF-8" ?> ";
  echo "<callxml version="2.0"> ";
  echo "  <block label=" $s "> ";

to
  echo '<?xml version="1.0" encoding="UTF-8" ?> ';
  echo '<callxml version="2.0"> ';
  echo '  <block label=" $s "> ';

the code will work correctly.
MattHenry
3/27/2006 6:42 PM (EST)


Hiya Vincent,

Yyet another instance of my docs having a misleading typo; I really should be more careful, as this could mislead the heck out of folks. Thanks for catching this for me; I'll have this corrected soon!

~Matthew Henry
bryanaamot
5/18/2006 2:09 PM (EDT)
Technically speaking, it is a good idea to specify the content type like this:

header('Content-Type: text/xml');

Note: This must appear before any other text is output.

login
  Introduction to Server Side Languages  |  TOC  |  tutorial Hello world with ColdFusion  

© 2008 Voxeo Corporation  |  Voxeo IVR  |  VoiceXML & CCXML IVR Developer Site