Created
October 19, 2012 16:29
-
-
Save Opus1no2/3919181 to your computer and use it in GitHub Desktop.
Short example of how to receive data from an Ifbyphone IVR and send an XML response.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* | |
* Short example of how receive post data from | |
* an Ifbyphone IVR and send a response after validation | |
* | |
* Example Usage: | |
* | |
* $example = new EmployeeValidationExample($_GET); | |
* echo $example->sendXmlResponse(); | |
*/ | |
class EmployeeValidationExample | |
{ | |
/** | |
* @var int $cid | |
*/ | |
public $cid; | |
/** | |
* @var int $_employeeId | |
*/ | |
protected $_employeeId; | |
/** | |
* | |
* Pass post or GET data to a method sets data | |
* | |
* @param array $options | |
* @return null | |
*/ | |
public function __construct(array $options) | |
{ | |
$this->_setOptions($options); | |
} | |
/** | |
* | |
* Set GET or POST data from constructor | |
* | |
* @param array $options | |
* @return null | |
*/ | |
protected function _setOptions($options) | |
{ | |
if (isset($options['EmployeeID'])) { | |
$this->setEmployeeId($options['EmployeeID']); | |
} | |
if (isset($options['cid'])) { | |
$this->setCid($options['cid']); | |
} | |
} | |
/** | |
* | |
* Set Employee Id | |
* | |
* @param int $employeeId | |
* @return null | |
*/ | |
public function setEmployeeId($employeeId) | |
{ | |
$this->_employeeId = $employeeId; | |
} | |
/** | |
* | |
* Get Employee Id | |
* | |
* @return int $this->_employeeId | |
*/ | |
public function getEmployeeId() | |
{ | |
return (int)$this->_employeeId; | |
} | |
/** | |
* | |
* Set Caller Id | |
* | |
* @param int $cid | |
* @return null | |
*/ | |
public function setCid($cid) | |
{ | |
$this->cid = $cid; | |
} | |
/** | |
* | |
* Get Caller Id | |
* | |
* @return int $this->cid | |
*/ | |
public function getCid() | |
{ | |
return (int)$this->cid; | |
} | |
/** | |
* | |
* List of valid Id's. This information | |
* would likely be stored in a database. | |
* | |
* @return array $validEmployeeIds | |
*/ | |
protected function _getValidId() | |
{ | |
$validEmployeeIds = array( | |
1234, 5678, 44556 | |
); | |
return $validEmployeeIds; | |
} | |
/** | |
* | |
* Determine if a valid Id was provided by the caller | |
* | |
* @return bool true | false | |
*/ | |
public function validateEmployeeId() | |
{ | |
if (in_array($this->getEmployeeId(), $this->_getValidId())) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
/** | |
* | |
* Build an XML response to route the caller to the next step of the call flow | |
* | |
* @param string $app | |
* @param int $app_id | |
* @param string $p_t | |
* @param array $user_params | |
* | |
* @return XML | |
*/ | |
public function buildXmlResponse($app, $app_id, $p_t = null, array $user_params = array()) | |
{ | |
$dom = new DOMDocument('1.0', 'utf-8'); | |
$response = $dom->appendChild(new DOMElement('action')); | |
$response->appendChild(new DOMElement('app', $app)); | |
$parameter = $response->appendChild(new DOMElement('parameters')); | |
$parameter->appendChild(new DOMElement('id', $app_id)); | |
$parameter->appendChild(new DOMElement('p_t', $p_t)); | |
if ($user_params) { | |
$user_parameter = $parameter->appendChild(new DOMElement('user_parameters')); | |
foreach ($user_params as $param => $value) { | |
$user_parameter->appendChild(new DOMElement($param, $value)); | |
} | |
} | |
return $dom->saveXML(); | |
} | |
/** | |
* | |
* Logic to determine which XML response to send | |
* | |
* @return XML | |
*/ | |
public function sendXmlResponse() | |
{ | |
if ($this->validateEmployeeId()) { | |
//Route to a Survo that handles a valid Id | |
return $this->buildXmlResponse('survo', 1234, null, array('first_name' => 'john')); | |
} else { | |
//Route to a Survo that handles invalid Id's | |
return $this->buildXmlResponse('survo', 5678, null, array('first_name' => 'john')); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment