CallXML 3.0 Development GuideHome  |  Frameset Home

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

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.


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="3.0">

  <do label="pound">
    <say>you pressed the pound key.</say>
  </do>

</callxml>


"HelloWorld-star.xml"

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

  <do label="star">
    <say>you pressed the star key.</tsay>
  </do>

</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="3.0">

  <do>
    <prompt value="helloworld"
              choices="*,#"/>

    <on event="choice:*">
      <goto value="helloworld-star.xml"
            submit="*"
            method="get"/>
    </on>

    <on event="choice:#">
      <goto value="helloworld-pound.xml"
            submit="*"
            method="get"/>
    </on>

  </do>

</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="3.0">

  <do>
    <prompt value="helloworld"
              choices="*,#"/>

    <on event="choice:*">
      <goto value="helloworld-handler.php?key=star" submit="*" method="get"/>
    </on>

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

  </do>

</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=\"3.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 "  <do label=\" $s \"> ";


This line begins our container element, and uses our "s" variable as the label name.


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


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 "  </do> ";
  echo "</callxml>";


These lines generate callxml that close our container element 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=\"3.0\"> ";
  echo "  <do label=\" $s \"> ";

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

  echo "  </do> ";
  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 3.0 source code.


What we covered




  ANNOTATIONS: EXISTING POSTS
rschoo
2/2/2007 10:38 AM (EST)
Your example is showing:
echo "<?xml version=/"1.0/" encoding=/"UTF-8/" ?> ";
echo "<callxml version=/"3.0/"> ";

Shouldn't this be:
echo "<?xml version=\"1.0\" encoding=\"UTF-8\" ?> ";
echo "<callxml version=\"3.0\"> ";

that is backslash instead of slash. When I changed this in my program (I copied your code) my program started working.

cheers reinier
RickG
2/2/2007 2:09 PM (EST)
Hi Reinier,

You're right, they should be backslashes.  I'll make sure the documentation is corrected.

Thank you for pointing this out!

-Rick G.
ruan
11/12/2007 12:58 AM (EST)
Hi,

I created this XML file:

<?xml version="1.0" encoding="UTF-8" ?>
<callxml version="3.0">
    <do choices="Option1,Option2,Option3">
    <say>
      Welcome to my menu. Please state your query now. You have 3 available options.
    </say>
    <wait value="10s"/>
    <on event="choice:Option1">
      <goto value="http://my_url.com/menu-handler.php?key=option1" submit="*" method="get"/>
    </on>
    <on event="choice:Option2">
      <goto value="http://my_url.com/menu-handler.php?key=option2" submit="*" method="get"/>
    </on>
    <on event="choice:Option3">
      <say> now arriving at $session.lastchoice;</say>
    </on>
  </do>
</callxml>

http://my_url.com/menu-handler.php?key=option1 returns:

<?xml version="1.0" encoding="UTF-8" ?> <callxml version="3.0">  <do label="option1">    <say>Welcome to option 1, the url for option 1 is http://some.url.com</say>  </do> </callxml>

If I choose option3 it works perfectly, but neither option1 nor option2 works - I just get disconnected. Any ideas?
mikethompson
11/12/2007 12:12 PM (EST)
Hi Ruan,

Without seeing application debugger logs, it's difficult for me to tell what's going wrong here.  However, to make sure the choice 1 and 2 events are getting hit at all, I would throw in some log lines.  For example:


  <on event="choice:Option1">
      <log> **************** OPTION 1 HIT ****************** </log>
      <goto value="http://my_url.com/menu-handler.php?key=option1" submit="*" method="get"/>
    </on>
    <on event="choice:Option2">
      <log> **************** OPTION 2 HIT ****************** </log>
      <goto value="http://my_url.com/menu-handler.php?key=option2" submit="*" method="get"/>
    </on>
    <on event="choice:Option3">
      <log> **************** OPTION 3 HIT ****************** </log>
      <say> now arriving at $session.lastchoice;</say>
    </on>

Based on the code snippet, I don't see a reason why option 1 and 2 aren't working.  Of course, this is assuming you are using real URLs in your true version of the code.

Best,
Mike Thompson
Voxeo Corporation




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

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