Docs

XML generation examples for different use cases

First of all you need to create an application:

http://api.example.com/v2/accounts/$\{accountSid}/applications
POST data:
{
  "controller":"https:/host.example.com/controller/voicing/xml/your_case.php",
  "method":"GET",
  "name":"your_name"
}

As a result an app_sid will be generated that can be later used in REST request "makecall". For example:

POST https://api.example.com/v2/accounts/$\{accountSid}/calls
POST data:
{
  "from": "from",
  "to": "to",
  "call_app_sid": "${app_sid}"
}

1. Callback case.

A two way call will be initiated. This is a REST request "makecall" and to_number in XML instruction as shown below:

<?php

// function description

// from number - this number will be shown as caller id (numbers rented from Apifonica only)
// to number - destination of the call
define('DEFAULT_FROM', 'from_number');
define('DEFAULT_TO', 'to_number');

// Array with XML contents
$lines = [];

// Function to assemble XML instructions
function getXml()
{
  global $lines;
  $lines[] = '<?xml version="1.0" encoding="UTF-8"?>';

  // Callback
  $lines[] = '' . DEFAULT_TO . ' ';
  $lines[] = '';
  return;
}
getXml();
if (!empty($lines)) {
  header('Content-type: text/plain; version=0.0.4');
  print_r(implode("", $lines));
}
?>

Adressing https:/host.example.com/controller/voicing/xml/callback.php an XML will be received in return, so Apifonica will be able to call to_number and after the call completes will be able to end it with endCall function:

<?xml version="1.0" encoding="UTF-8"?>
  <response>
    <makeCall from="from_number">
      <number>to_number</number>
    </makeCall>
    <endCall/>
  </response>

(In the subsequent cases an XML example will be displayed in a second part of code example by default)

2. Consecutive call

<?php
// Description
// from number - this number will be shown as caller id (numbers rented from Apifonica only)
// to number - destination of the call
define('DEFAULT_FROM', 'from_number');
define('DEFAULT_TO', 'to_number');
define('DEFAULT_FROM2', 'from_number2');
define('DEFAULT_TO2', 'to_number2');
// Array with XML contents
$lines = [];

// Function to assemble XML instructions
function getXml()
{
  global $lines;
  $lines[] = '<?xml version="1.0" encoding="UTF-8"?>';
  // Сonsecutive call execution without timeout
  $lines[] = '' . DEFAULT_TO . ' ' . DEFAULT_TO2 . ' '; $lines[] = '';
}
getXml();
if (!empty($lines)) {
  header('Content-type: text/plain; version=0.0.4');
  print_r(implode("", $lines));
}
?>

XML:

<?xml version="1.0" encoding="UTF-8"?>
  <response>
    <makeCall from="from_number">
      <number>to_number</number>
    </makeCall>
    <makeCall from="from_number2">
      <number>to_number2</number>
    </makeCall>
    <endCall/>
  </response>

3. Parallel call


// Description

// from number - this number will be shown as caller id (numbers rented from Apifonica only)
// to number - destination of the call
define('DEFAULT_FROM', 'from_number');
define('DEFAULT_TO', 'to_number');
define('DEFAULT_TO2', 'to_number2');
define('DEFAULT_TO3', 'to_number3');

// Array with XML contents
$lines = [];

// Function to assemble XML instructions
function getXml()
{
  global $lines;
  $lines[] = '<?xml version="1.0" encoding="UTF-8"?>';

  // Call execution without timeout
  $lines[] = '' . DEFAULT_TO . ' ' . DEFAULT_TO2 . ' ' . DEFAULT_TO3 . ' ';
  $lines[] = '';
}
getXml();
if (!empty($lines)) {
  header('Content-type: text/plain; version=0.0.4');
  print_r(implode("", $lines));
}
?>

XML:

<?xml version="1.0" encoding="UTF-8"?>
  <response>
    <makeCall from="from_number" timeout="15">
      <number>to_number</number>
      <number>to_number2</number>
      <number>to_number3</number>
    </makeCall>
    <endCall/>
  </response>

4. Play audio

In REST request "makecall" we can play audio file for the customer



<?php
// Description

// Array with XML contents
$lines = [];

// Function to assemble XML instructions
function getXml()
{
  global $lines;
  $lines[] = '<?xml version="1.0" encoding="UTF-8"?>';

  // Setting audio to play
  $lines[] = 'https://host.com/controller/voicing/audio/music.wav';
  $lines[] = '';
  $lines[] = '';
  return;
}
getXml();
if (!empty($lines)) {
  header('Content-type: text/plain; version=0.0.4');
  print_r(implode("", $lines));
}
?>

XML:

<?xml version="1.0" encoding="UTF-8"?>
  <response>
    <playAudio loops="0">https://host.com/controller/voicing/audio/music.wav</playAudio>
    <endCall/>
  </response>

5. Playing looped audio (2 times) and send SMS afterwars.



<?php
// Description
// from number - this number will be shown as caller id (numbers rented from Apifonica only)
// to number - destination of the call
define('DEFAULT_FROM_SMS', 'from_number_sms'); // required parameter - Only number type "messaging" can be used
define('DEFAULT_TO_SMS', 'to_number_sms');
// Array with XML contents
$lines = [];
// Function to assemble XML instructions
function getXml()
{
  global $lines; $lines[] = '<?xml version="1.0" encoding="UTF-8"?>';
  // Send message with XML instructions
  $lines[] = 'https://xml.example.com/controller/voicing/audio/music.wav ';
  $lines[] = '';
  return;
}
getXml();
if (!empty($lines)) {
  header('Content-type: text/plain; version=0.0.4');
  print_r(implode("", $lines));
}
?>

XML:

<?xml version="1.0" encoding="UTF-8"?>
  <response>
    <playAudio loops="1">https://xml.example.com/controller/voicing/audio/music.wav</playAudio>
    <sendMessage from="from_number_sms" to="to_number_sms" text="SMS text"</sendMessage>
    <endCall/>
  </response>

6. Interactive voice response

A bit more complicated example.

There are 2 modes:

action= one (receiving the use instruction which is initiated once the script was accessed for the first time)

and action= two (accepting user’s entered values) User can enter 4 buttons (keys 1,2,4 and 5)

Herewith timers are set (time until first key was pressed and time between presses)

If there is no interaction from user, script loops to the start.


<?php
// Description
// from number - this number will be shown as caller id (numbers rented from Apifonica only)
// to number - destination of the call
define('DEFAULT_FROM', 'from_number');
define('DEFAULT_FROM2', 'from_number2');
define('DEFAULT_FROM_SMS', 'from_number_sms'); // required parameter - Only number type "messaging" can be used
define('DEFAULT_TO', 'to_number');
define('DEFAULT_TO2', 'to_number2');
define('DEFAULT_TO_SMS', 'to_number_sms');
// Array with XML contents
$lines = [];
// Function to assemble XML instructions
function getXml()
{
global $lines;
$lines[] = '<?xml version="1.0" encoding="UTF-8"?>';
// ivr call
if ($_GET['xml_variant'] == 'case_8') {
  switch ($_GET['action']) {

  case 'one':
  $lines[] = 'https://xml.example.com/controller/voicing/audio/welcome_message.wav ';
  $lines[] = '';
  return;

  case 'two':
  //if nothing pressed, the code below is executed
  if (!$_GET['input']) {
    $lines[] = 'https://xml.example.com/controller/voicing/audio/nothing_pressed.wav'; $lines[] = ''; return; }

    switch ($_GET['input']) {
      case 1: $lines[] = '' . DEFAULT_TO . ''; $lines[] = ''; return;
      case 2: $lines[] = '' . DEFAULT_TO2 . ''; $lines[] = ''; return;
      case 4: $lines[] = ''; $lines[] = ''; return;
      case 5: $lines[] = 'https://xml.example.com/controller/voicing/audio/wake_me_up.wav '; $lines[] = ''; return;
      // if there were no overlaps, return to the start of IVR
      default: $lines[] = 'https://xml.example.com/controller/voicing/audio/no_match.wav '; $lines[] = ''; return; }
    }
  }
}
getXml();
if (!empty($lines)) {
  header('Content-type: text/plain; version=0.0.4');
  print_r(implode("", $lines));
}
?>

XML:

<?xml version="1.0" encoding="UTF-8"?>
  <response>
    <playAudio loops="1">https://xml.example.com/controller/voicing/audio/welcome_message.wav</playAudio>
    <getInput controller="https://xml.example.com/controller/voicing/xml/IVR.php?xml_variant=case_8&action=two" method="GET" timeout="5" timeoutGeneral="5"/>
  </response>