« Prev Section | Messaging |
Let's learn how to do SOAP integration by creating a new application.
For our example application, let's first drag out a Yes or No module to your workspace and enter the following text in the textbox: “Do you like animals? Press 1 for yes or 2 for no.”
From here, drag out a SOAP Webservice module to your workspace and connect it to your Yes or No module. For our WSDL, we're going to write the following: http://quickfuseapps.com/docs/examples/ws.php?WSDL. Note that when you click away from the WSDL, a method and parameters become available for you to use. The method available is “testMethod” and the parameter that is available is “answer”. The way this WSDL works is if “yes” is returned to the SOAP webservice, the result that gets returned is “animallover”. If “no” is returned to the SOAP webservice, the result that gets returned is “animalhater”.
Drag out a Branch on String module to your workspace and connect it to your SOAP Webservice module. For the variable in your Branch on String module, select soapResult. For the Value, enter “animallover” into the textbox. For the default node of your Branch on String module, drag out a Simple Prompt module and connect it to the node. In the textbox, enter the following text: “You're an animal hater.” To finish off that branch, drag out an Exit App module and connect it to the Simple Prompt module. For the “animallover” node of your Branch on String module, drag out a Simple Prompt module and connect it to the node. In the textbox, enter the following text: “You're an animal lover. Good for you.” To finish off that branch, drag out a Hang Up or Exit module and connect it to the Simple Prompt module.
From this example, the user is prompted to answer the question, “Do you like animals? Press 1 for yes or 2 for no.” If the user enters 1 for yes, the WSDL returns “animallover” and the application branches to the message, “You're an animal lover. Good for you.” If the user enters 2 for no, the WSDL returns “animalhater” and the application branches to the message, “You're an animal hater.”
For your reference, we have provided some sample PHP code that you can use to build your webservice.
NOTE: Please note that Plum Fuse supports return types that are simple, e.g., string or integer, or simple objects, or an array of simple objects. Objects with member variables that are also objects are not supported. Also, Plum Fuse does not provide support for authentication (WS-Security, SOAP Header Authentication, etc.).
UPDATE (1/27/11): We now offer a SOAP web service testing tool to allow you to check your web service against. For a link to this SOAP web service tester, please see here.
Let's take a look at a SOAP webservice example that returns a complex object; in this case, we'll return an array of strings.
For our application, first drag a Simple Menu module to your workspace and connect it to your Start module. In the textbox of the module, enter the following text: “Would you like to know the race results for Group A or Group B? Enter 1 for Group A or 2 for Group B.” Click on the + button to add an additional option to your Simple Menu module.
Next, drag a Multiple Choice module to your workspace and connect it to the node for “1” on your Simple Menu module. In the textbox of your Multiple Choice module, enter the following text: “For mike, press 1. For john, press 2. For samantha, press 3.” Click on the + button twice to add two additional options to your Multiple Choice module.
Next, drag a Multiple Choice module to your workspace and connect it to the node for “2” on your Simple Menu module. In the textbox of your Multiple Choice module, enter the following text: For joe, press 1. For bob, press 2. For sally, press 3.” Click on the + button twice to add two additional options to your Multiple Choice module.
From there, drag out a SOAP Webservice module to your workspace and connect the nodes from your Multiple Choice modules to the receptor of your SOAP Webservice module. In the SOAP Webservice, we're going to enter our WSDL URL. Our WSDL code consists of the following:
<?php include_once('WSDL_Gen.php'); $NAMESPACE = "http://quickfuseapps.com"; $WS_DOC_CSS = "ws_doc.css"; $DESCRIPTION = "An example of how to use WSDL_Gen.php"; /** * This is a web service class that implements one simple function * named runnerMethod. This function receives one string called $answer * and returns an array of strings. If the $answer string is equal to 1, * it returns the array for mike, john, and samantha. If the $answer string * is not equal to 1, it returns the array for joe, bob, and sally. **/ class SampleWebservice extends Services_Webservice { /** * * @param string $answer * @return string[] **/ function runnerMethod($answer) { if ($answer == '1'){ return array("mike" => "third", "john" => "second", "samantha" => "first"); } return array("joe" => "fifth", "bob" => "sixth", "sally" => "fourth"); } } $myService = new SampleWebservice($NAMESPACE, $DESCRIPTION, array('uri'=>$NAMESPACE, 'encoding'=>SOAP_ENCODED)); $myService->handle(); ?>
Note that the method, runnerMethod, comes up for our module. For the Parameter, we're going to set it as “answer” and for the value, we're going to set it as menuChoice.
Next, drag out a Branch on Number module to your workspace and set the variable to be menuChoice and compare it to the value, 2. Then, drag out a Get Row from Stack module to your workspace and connect it to the “2” node for the Branch on Number module. For your Get Row from Stack module, be sure to select the options for “Reassign names” and “Get a specific row #”. For “Get the _ row”, change it to a variable and select the variable, multChoice2. Next, drag out a Reserve Variable module to your workspace and name it result. In the Get Row from Stack module, set the Variable to be result and the Fieldname to be arrayItem.
From there, drag out a Multipart Prompt module to your workspace and for the first textbox, enter the following text: “The runner came in”. Click on the + button to add an additional phrase/variable to the module and convert it to a variable. For the variable, set it to result. Finally, close off that branch of the application by dragging a Hang Up or Exit module to your workspace and connect it to your Multipart Prompt module.
For the Default node of your Branch on Number module, drag out a Get Row from Stack module to your workspace and connect it to the node. Similar to what you did for the other Get Row from Stack module, select the options for “Reassign names” and “Get a specific row #”. For “Get the _ row”, change it to a variable and select the variable, multChoice. In the Get Row from Stack module, set the Variable to be result and the Fieldname to be arrayItem.
From there, drag out a Multipart Prompt module to your workspace and for the first textbox, enter the following text: “The runner came in”. Click on the + button to add an additional phrase/variable to the module and convert it to a variable. For the variable, set it to result. Finally, close off that branch of the application by dragging a Hang Up or Exit module to your workspace and connect it to your Multipart Prompt module.
Your final application should look like the following:
For REST integration, the Fetch from URL module can be used to return values from the following options: text, JSON, CSV, and XML. Let's first examine a practical use of a typical REST service, first demonstrating how to return JSON for the Fetch from URL module using a dynamically built URL from the Build URL module.
The REST service we're using here is served from the base URL http://quickfuseapps.com/docs/examples/rest
and functions as a “bookstore”.
<?php /************* * Plum Fuse SAMPLE REST WEBSERVICE * This webservice demonstrates how to make a REST service for use with the * 'Fetch from URL' module in Plum Fuse. * (c) 2010 Plum Voice *************/ $ret = ''; $answer = ''; /** * The various strings that can be returned by this webservice (text, JSON, XML, or CSV) **/ $text_yes = 'animal lover'; $text_no = 'animal hater'; $json_yes = '{ "foo": { "bar": "animal", "baz": "lover", "arrayofvalues": [ "answer", "accepted" ], "arrayofobjects": [ { "var1": "hello" }, { "var2": "world" } ] } }'; $json_no = '{ "foo": { "bar": "animal", "baz": "hater", "arrayofvalues": [ "answer", "rejected" ], "arrayofobjects": [ { "var1": "hello" }, { "var2": "world" } ] } }'; $xml_yes = '<foo accepted="yes"> <bar>animal</bar> <bar>lover</bar> <baz>hello</baz> <baz>world</baz> </foo>'; $xml_no = '<foo accepted="no"> <bar>animal</bar> <bar>hater</bar> <baz>hello</baz> <baz>world</baz> </foo>'; $csv_yes = 'foo,bar animal,lover hello,world answer,accepted'; $csv_no = 'foo,bar animal,hater hello,world answer,rejected'; /** * Get the values for 'answer', and 'ret' (the expected return type) * If both GET and POST variables are set, POST overrides GET */ if( isset( $_GET['ret'] ) && !isset( $_POST['ret'] ) ) { $ret = strtoupper( $_GET['ret'] ); } elseif( isset( $_POST['ret'] ) ) { $ret = strtoupper( $_POST['ret'] ); } if( isset( $_GET['answer'] ) && !isset( $_POST['answer'] ) ) { $answer = $_GET['answer']; } elseif( isset( $_POST['answer'] ) ) { $answer = $_POST['answer']; } /** * Return the proper format (text/string, JSON, XML, CSV) */ switch( $ret ) { case 'TEXT': if( strtoupper( $answer ) == 'YES' ) echo $text_yes; else echo $text_no; break; case 'JSON': if( strtoupper( $answer ) == 'YES' ) echo $json_yes; else echo $json_no; break; case 'XML': if( strtoupper( $answer ) == 'YES' ) echo $xml_yes; else echo $xml_no; break; case 'CSV': if( strtoupper( $answer ) == 'YES' ) echo $csv_yes; else echo $csv_no; break; } exit; ?>
You can ask it for information on various books using a GET to the URLs books/
or books/<id>
. To purchase a book, you send an HTTP POST to the URL books/<id>/buy/<qty>
. All requests need to send along a GET or POST parameter format
, which specifies one of json
, xml
, or csv
. We'll focus on the json
format first.
First, drag a Multiple Choice module to your workspace and connect it to your Start module. In the textbox of the module, enter the following: “Which book would you like to purchase? For “The Girl With The Dragon Tattoo,” press one or say, “The Girl With The Dragon Tattoo.” For “Wuthering Heights,” press two or say, “Wuthering Heights.” For “Ender's Game,” press three or say, “Ender's Game.” Then, click on the plus button twice to add two additional choices to your module. For choice 1, enter “The Girl With The Dragon Tattoo” in the Or say textbox. For choice 2, enter “Wuthering Heights” in the Or say textbox. For choice 3, enter “Ender's Game” in the Or say textbox.
Next, drag a Digits Input module to your workspace and connect it to your Multiple Choice module. In the textbox of the module, enter the following: “How many copies of the book would you like to purchase?” For min digits, enter 1 in the textbox. For max digits, enter 1 in the textbox.
Next, we're going to introduce a new module: the Build URL module. The Build URL module is used for constructing URLs dynamically using Plum Fuse variables and is particularly useful for REST integration. To continue with our application example, drag out a Build URL module to your workspace and connect it to your Digits Input module. In the base URL textbox, enter the following URL: http://quickfuseapps.com/docs/examples/rest. Then, click on the plus button 3 times to add 3 additional path elements to your module. For the first path element, enter “books” in the textbox and leave the Transform string set to “URL-encode.” For the second path element, click on the toggler to set it to a variable and enter “multChoice” as the variable. Leave the Transform string set to “URL-encode.” For the third path element, enter “buy” in the textbox and leave the Transform string set to “URL-encode.” For the last path element, click on the toggler to set it to a variable and enter “digits” in the textbox. Leave the Transform string set to “URL-encode.” With these path elements, we can build our URL dynamically based on the user's input for our first two modules.
Next, we're going to drag out a Fetch from URL to our workspace and connect it to our Build URL module. Click on the toggler for the URL to change it to a variable and enter “url” as the variable (this allows us to use the url that's dynamically built from the Build from URL module). Select POST from the drop-down menu and select JSON as what gets returned from the module. For the POST parameter, enter “format” in the textbox. For the POST value, enter “json” in the textbox. Next, select “Array of values → Stack” from the “Extract:” drop-down menu. For “Located at: obj”, enter the following in the textbox: .PURCHASE.
From here, we're going to drag a Get Row from Stack module to our workspace and connect it to our Fetch from URL module. For the Get Row from Stack module, go to the Menu Options and make sure the following two options are selected: Reassign names and Get a specific row #. For Get the _ Row, enter 3 in the textbox. Since we're going to need to reference a variable in our Get Row from Stack module, drag out a Reserve Variable module to your workspace and declare a variable, myvar. Now, go back into your Get Row from Stack module and enter myvar in the Variable textbox. For Fieldname, select arrayItem as the field name. NOTE: We must use arrayItem in this case for array of values as this is the only way we can reference values in the stack when extracting an array of values from JSON.
Next, drag a Multipart Prompt module to your workspace and connect it to your Get Row from Stack module. In the textbox of the Multipart Prompt module, enter the following text: “You purchased”. Click on the plus button to add a phrase/variable to your Multipart Prompt module and click on the toggler to convert it to a variable. Select digits as the variable. Click on the plus button to add a phrase to your Multipart Prompt module. In the textbox, enter the following text: “copies for a total of”. Click on the plus button again to add a phrase/variable to your Multipart Prompt module and click on the toggler to convert it to a variable. Select myvar as the variable. Click on the plus button one last time to add a phrase to your Multipart Prompt module. In the textbox, enter the following text: “dollars.”
Finally, complete your application by dragging a Hang Up or Exit module to your workspace and connecting it to your Multipart Prompt module.
From this example, a user would call into the application, select a book, and then select how many copies of the book they would like to purchase. From there, the application builds a URL from the user's responses and directs it to the Fetch from URL module, which posts the parameter, “format”, with the value, “json”. The module returns a JSON object with an array of values, which gets placed into a stack. From there, the Get Row from Stack module is used to reference a value from the stack (in this case, we want to reference the price) and stored in the variable, myvar. From there, the Multipart Prompt module states to the user the number of copies that were purchased and the total price.
For our next example, we're going to demonstrate how to return CSV from our Fetch from URL module. For this example, we're going to drag out a Multiple Choice module to our workspace and connect it to your Start module. In the textbox of the Multiple Choice module, enter the following text: “Which book would you like to know the author of? For “The Girl With The Dragon Tattoo,” press one or say, “The Girl With The Dragon Tattoo.” For “Wuthering Heights,” press two or say, “Wuthering Heights.” For “Ender's Game,” press three or say, “Ender's Game.” Then, click on the plus button twice to add two additional choices for your Multiple Choice module. For your first choice, enter “The Girl With The Dragon Tattoo.” For your second choice, enter “Wuthering Heights.” For your third choice, enter “Ender's Game.”
Next, drag a Build URL module to your workspace and connect it to your Multiple Choice module. In the base URL, enter the following URL: http://quickfuseapps.com/docs/examples/rest. Then, click on the plus button to add an additional path element to your module. For the first path element, enter “books” in the textbox and leave the Transform string set to “URL-encode.” For the second path element, click on the toggler to set it to a variable and enter multChoice as the variable. Leave the Transform string set to “URL-encode.” Then, click on the plus button to add a query parameter to your module. For the query parameter, enter “format” in the textbox. For the value, enter “csv” in the textbox.
Next, drag a Fetch from URL module to your workspace and connect it to your Build URL module. Click on the toggler for the URL to change it to a variable and enter “url” as the variable. Select GET from the drop-down menu and select CSV as what gets returned from the module. Keep the Separator for the module as ',' and the Quotes as '”'. Also, make sure the option for “Use the 1st row as the header” is selected from your drop-down menu. Keep in mind that when returning CSV from this module, it will return multiple rows in a stack.
From here, drag a Get Row from Stack module to your workspace and connect it to your Fetch from URL module. For the menu options for your Fetch from URL module, make sure that the option for “Reassign names” is selected and the option for “Get a specific row #” is selected. For Get The _ Row, enter “1” in the textbox. Also, since we're going to need to reference a variable in our Get Row from Stack module, drag out a Reserve Variable module to your workspace and declare a variable, myvar. Now, go back into your Get Row from Stack module and enter myvar in the Variable textbox. For Fieldname, enter “AUTHOR” as the field name. This allows us to extract the value for AUTHOR so that we can return the author of the book that was selected.
Next, drag a Multipart Prompt module to your workspace and connect it to your Get Row from Stack module. In the textbox, enter the following text: “The author of that book is”. Click on the plus button to add a phrase/variable to your module and click on the toggler to make it a variable. Enter myvar as the variable. Finally, drag a Hang Up or Exit module to your workspace and connect it to your Multipart Prompt module.
From this example, the application prompts the user to enter a book that they would like to know the author of. Once the user enters a book, the application proceeds to the Build URL module, which dynamically creates the URL for the Fetch from URL module based on the user's choice of book. Then, in the Fetch from URL module, the module returns csv and uses the 1st row as the header. From here, the application goes to the Get Row from Stack module and extracts the value for AUTHOR from the csv. Finally, the Multipart Prompt module tells the user the author of the book that was chosen.
For our last practical example, we're going to demonstrate how to return XML from our Fetch from URL module.
First, drag out a Multiple Choice module to your workspace and connect it to your Start module. In the textbox of the Multiple Choice module, enter the following text: “Which book would you like to know the price of? For “The Girl With The Dragon Tattoo,” press one or say, “The Girl With The Dragon Tattoo.” For “Wuthering Heights,” press two or say, “Wuthering Heights.” For “Ender's Game,” press three or say, “Ender's Game.” Then, click on the plus button twice to add two additional choices for your Multiple Choice module. For your first choice, enter “The Girl With The Dragon Tattoo.” For your second choice, enter “Wuthering Heights.” For your third choice, enter “Ender's Game.”
Next, drag a Build URL module to your workspace and connect it to your Multiple Choice module. In the base URL, enter the following URL: http://quickfuseapps.com/docs/examples/rest. Then, click on the plus button to add an additional path element to your module. For the first path element, enter “books” in the textbox and leave the Transform string set to “URL-encode.” For the second path element, click on the toggler to set it to a variable and enter multChoice as the variable. Leave the Transform string set to “URL-encode.” Then, click on the plus button to add a query parameter to your module. For the query parameter, enter “format” in the textbox. For the value, enter “xml” in the textbox.
Next, drag a Fetch from URL module to your workspace and connect it to your Build URL module. Click on the toggler for the URL to change it to a variable and enter “url” as the variable. Select GET from the drop-down menu and select XML as what gets returned from the module. For “Get:”, select “Matched node values → Stack” from the drop-down menu. In the “for XPath: /” textbox, enter ”/books/book/price”.
From here, drag a Get Row from Stack module to your workspace and connect it to your Fetch from URL module. For the menu options for your Fetch from URL module, make sure that the option for “Reassign names” is selected and the option for “Get the next row” is selected. Also, since we're going to need to reference a variable in our Get Row from Stack module, drag out a Reserve Variable module to your workspace and declare a variable, myvar. Now, go back into your Get Row from Stack module and enter myvar in the Variable textbox. For Fieldname, select nodeValue as the field name. This allows us to extract the value (in this case, the price of the selected book) from our XML. NOTE: We must use nodeValue in this case for matched node values as this is the only way we can reference values in the stack when extracting matched node values from XML.
Next, drag a Multipart Prompt module to your workspace and connect it to your Get Row from Stack module. In the textbox, enter the following text: “The price of that book is”. Click on the plus button to add a phrase/variable to your module and click on the toggler to make it a variable. Enter myvar as the variable. Click on the plus button again to add a phrase to your Multipart Prompt module and enter “dollars” in the textbox. Finally, drag a Hang Up or Exit module to your workspace and connect it to your Multipart Prompt module.
From this example, the application prompts the user to enter a book that they would like to know the price of. Once the user enters a book, the application proceeds to the Build URL module, which dynamically creates the URL for the Fetch from URL module based on the user's choice of book. Then, in the Fetch from URL module, the module returns XML and places the matched node values into a stack. From here, the application goes to the Get Row from Stack module and extracts the value from the stack. Finally, the Multipart Prompt module tells the user the price of the book that was chosen.
Let's start with an example for our Fetch from URL module where we'll return text. First, drag a Fetch from URL module to your workspace and connect it to the Start module.
Next, let's add the following GET URL to our Fetch from URL module: http://quickfuseapps.com/docs/examples/rest.php?ret=text&answer=no
In this URL, we just return a simple string with the following text: “You are an animal hater!”
We can see this string get returned in our application by attaching a Say Variable module to our Fetch from URL module and setting the variable to restResult. From there, connect a Hang Up or Exit module to the Say Variable module to finish the application.
From this example, the Fetch from URL module would return the string, “You are an animal hater!”, and store it in restResult. From there, the Say Variable module would state to the user, “You are an animal hater!”
Now, let's try using a POST URL for our Fetch from URL module example, still returning text. For the POST URL, we're going to enter http://quickfuseapps.com/docs/examples/rest.php. For our POST parameters, we're going to set one of our parameter names as ret with a value of “text” and our other parameter name as answer with a value of “no”. Again, we're going to attach a Say Variable module to our Fetch from URL module with a variable of restResult and then attach a Hang Up or Exit module to the end of the Fetch from URL module.
From this example, we send two POST parameters, ret and answer, with the values, “text” and “no” respectively. What gets returned in restResult is the string, “You are an animal hater!” This message is stated to the user from the Say Variable module.
Now that we have learned how to return text for the Fetch from URL module, let's learn how to return JSON. Click on the drop-down menu for “Returns:” and select “JSON”. We're also going to switch our module back to using a GET URL.
We're going to begin with returning just one value for our JSON example (NOTE: for “Extract:”, we have “One value” selected from the drop-down menu). First, for our GET URL, enter the following: http://quickfuseapps.com/docs/examples/rest.php?ret=json&answer=yes. For our URL, we are returning the following JSON object:
{ "foo": { "bar": "animal", "baz": "lover" } }
Our object in this case is “foo” with the members, “bar” and “baz”. Within the member, “bar”, is the value, “animal”, and within the member, “baz”, is the value, “lover”.
For our Fetch from URL module, in the textbox of “Located at: obj”, enter the following text: ”.foo.baz”. Next, connect a Say Variable module to the Fetch from URL module with variable, restResult. Finally, connect a Hang Up or Exit module to the Say Variable module.
From this example, we reference the value of “baz” using the notation, ”.foo.baz”. When the user reaches the Say Variable module, they will hear, “lover”.
For our next JSON example, we're going to learn how to return an object, place it into a stack, and return a specific value from a row in that stack (NOTE: for “Extract:”, we have “Object → Stack” selected from the drop-down menu). However, for this example, we're going to use POST and for our URL, we're going to enter the following: http://quickfuseapps.com/docs/examples/rest.php. We're going to set two POST parameters: for one parameter, “ret”, we're going to set a value of “json” and for our other parameter, “answer”, we are going to set a value of “yes”. For the “Located at: obj” textbox, we reference our object, foo, by entering ”.foo” in the textbox. From there, we attach a Get Row from Stack module to our workspace and then attach a Say Variable module to that module. To close out the application, attach a Hang Up or Exit module to the Say Variable module.
From this example, when we send our two POST parameters, ret and answer with the values “json” and “yes” respectively, it returns to us an object that all gets placed into one row in a stack. To reference this stack, we use the Get Row from Stack module. In our Get Row from Stack module, we are going to reference a variable, var1, and assign it with a fieldname of baz (just for example purposes, baz is one of the members of our object). From there, we use the Say Variable module to say to the user the value of baz, which happens to be the string, “lover”.
Next, let's learn how to use JSON for an array of objects that gets placed into a stack (NOTE: for “Extract:”, we have “Array of objects → Stack” selected from the drop-down menu). For this example, let's switch it back to using GET and use a GET URL of: http://quickfuseapps.com/docs/examples/rest.php?ret=json&answer=no.
Note that our URL returns the following:
{ "foo": { "bar": "animal", "baz": "hater", "arrayofvalues": [ "answer", "rejected" ], "arrayofobjects": [ { "var1": "hello" }, { "var2": "world" } ] } }
In our “foo” object, let's say that we want to pull the value, “world”, from the member, “var2”, which is inside of “arrayofobjects”. To reference “arrayofobjects” from our Fetch from URL module, we input ”.foo.arrayofobjects” in the “Located at: obj” textbox. Note that this will place the contents of “arrayofcontents” into a stack. Again, we use the Get Row from Stack module here, but in this case, we will need to specify the correct row in order to extract “world”. For the Get Row from Stack module, we will need to select “Get a specific row” from our menu options and enter “2” for “Get the _ row”. From there, we can set a variable, myvar, and set it to the member, “var2”. Finally, we use a Say Variable module and enter the variable, myvar, which returns the string value, “world” back to the user.
For our last example with JSON, let's learn how to work with an array of values that gets placed into a stack (NOTE: for “Extract:”, we have “Array of values → Stack” selected from the drop-down menu). Also, we're going to reference our array of values by entering ”.foo.arrayofvalues” in the “Located at: obj” textbox. We're going to leave the rest of our Fetch from URL module from the last example alone. Note that this will place the contents of “arrayofvalues” into a stack.
Once again, we're going to use the Get Row from Stack module to reference our stack. For example purposes, let's say that we want to extract the value, “rejected”, from our array of values. To do that, we need to again specify the correct row (in this case, it's “2”). However, please NOTE that in order for us to reference the value, “rejected”, we will need to set a variable in our Get Row from Stack module (let's call it myvar) and set it equal to arrayItem. From there, we can again use the Say Variable module and enter the variable, myvar, which returns the string value, “rejected”, to the user.
Now that we've learned how to use JSON for our Fetch from URL module, let's learn how to use CSV. Click on the drop-down menu for “Returns:” and select “CSV”. We're going to leave many of the default settings for the module in place and use GET with a GET URL of http://quickfuseapps.com/docs/examples/rest.php?ret=csv&answer=no. We are also going to select the option, “Use the 1st row as the header”.
Note that our GET URL returns the following:
foo,bar animal,hater hello,world answer,rejected
Let's say that we want to return “world” from our GET URL. To do this, we will first need to attach a Get Row from Stack module to our Fetch from URL module since the Fetch from URL module will return multiple rows into a stack. Since we are using the option, “Use the 1st row as the header”, we will need to specify the Get Row from Stack module to get the second row and then set a variable, myvar, to equal “bar”. From there, we attach a Say Variable module to the Fetch from URL module and select myvar as the variable, which would return “world” back to the user.
Now that we've learned how to return CSV from the Fetch from URL module, let's learn how to return XML. Click on the drop-down menu for “Returns:” and select “XML”. For our example, we're going to use GET and our GET URL will be: http://quickfuseapps.com/docs/examples/rest.php?ret=xml&answer=no. For “Get:”, we are going to select “1st matching node value” as the option from the drop-down menu.
Note that when you enter the GET URL in a browser and view the source code, it returns the following:
<foo accepted="no"> <bar>animal</bar> <bar>hater</bar> <baz>hello</baz> <baz>world</baz> </foo>
As a simple example, let's say that we want to return “animal”. To reference it, we would input the following in the “for Xpath: /” textbox:
foo//bar
This would reference both “bar” elements from our parent element, “foo”. However, since we selected the option for “1st matching node value”, it will only match to the first “bar” element in our XML and return “animal”. To complete our application example, we attach a Say Variable to the end of our Fetch from URL module and set the variable as restResult and then attach a Hang Up or Exit module to that module.
For our next XML example, we're going to demonstrate how to use the “Matched node values → Stack” option for our Fetch from URL module. We're going to continue using GET for our module with the GET URL: http://quickfuseapps.com/docs/examples/rest.php?ret=xml&answer=no.
However, instead of returning the value, “animal”, this time, let's return the value, “world”. To do this, we will need to enter the following in the “for Xpath: /” textbox:
foo//baz
This will return all the matched values for “baz” from our parent element, “foo”.
For this example, since we selected the option, “Matched node values → Stack”, from the “Get:” drop-down menu, the Fetch from URL module will return matched node values and place them into a stack. To extract the value from the stack we will need to use the Get Row from Stack module. Here, we will need to select a specific row from the stack (in this case, we want to input 2 for “Get the _ row”) in order to extract . Also, in our Get Row from Stack module, please NOTE that in order for us to reference the value, “world”, we will need to set a variable (again, in this case, we're calling it myvar) in our Get Row from Stack module and set it equal to nodeValue. From there, we can again use the Say Variable module and enter the variable, myvar, which would return the string value, “world”, to the user.
Now that we have learned how to return matched node values and place them onto a stack, let's continue with another XML example, this time using the option for “Matched elems' attributes → Stack”. We're going to leave the other options for our module the same and continue using the same GET URL: http://quickfuseapps.com/docs/examples/rest.php?ret=xml&answer=no
For our example, let's say that we want to return the value, “no”, from the attribute, “accepted”, in “foo”. To do this, we will need to enter the following in the “for Xpath: /” textbox:
foo
Again, we are using a Get Row from Stack module to extract the value, “no”, from the attribute, “accepted”. To do this, we set a variable, myvar, and set it to “accepted”. From here, our Say Variable module would be set to myvar, which would return “no” to the user.
For our last example of XML, we're going to select the option of “Matched elems' children → Stack”. Again, we're going to keep all of our module settings the same. Please note that when using elements of the same name, the module will match to last matching element in the XML.
Let's say that we want to extract a value from the element, “baz”. To do this, we need to enter the following in the “for Xpath: /” textbox:
/foo
This will pull all matching elements from the parent, “foo”, and place them onto a stack.
For our Get Row from Stack module, we're going to set a variable, myvar, and set it equal to “baz”. Just as a reminder, from our GET URL: http://quickfuseapps.com/docs/examples/rest.php?ret=xml&answer=no
we have the following:
<foo accepted="no"> <bar>animal</bar> <bar>hater</bar> <baz>hello</baz> <baz>world</baz> </foo>
Note that we have two “baz” elements. In this case, the Get Row from Stack module will only pull the last matching element in the XML (in this case, myvar would return “world” as that's the last value assigned to the matching element, “baz”).
Next Section » | VXML Subdialog |