/OpenVBX/libraries/VoiceVault.php

https://github.com/MichaelMackus/OpenVBX · PHP · 157 lines · 96 code · 19 blank · 42 comment · 6 complexity · 1b476d73fe8fac0e9e23cc1bb765e841 MD5 · raw file

  1. <?php
  2. /**
  3. * VoiceVault library.
  4. **/
  5. class VoiceVault
  6. {
  7. /**
  8. * @var boolean $debug Is the voice vault account in debug mode?
  9. */
  10. public $debug = false;
  11. /**
  12. * @var array $_auth Authentication values for the voice vault API.
  13. */
  14. private $_auth;
  15. /**
  16. * @var string $_api_url Base API URL.
  17. */
  18. private $_api_url;
  19. /**
  20. * Initialization. Sets the voice vault API URLs and parameters.
  21. */
  22. function __construct($username,
  23. $password,
  24. $organisation_id,
  25. $config_id,
  26. $debug=true,
  27. $api_url=null)
  28. {
  29. // Initialization
  30. $this->debug = $debug;
  31. $this->_auth = array(
  32. 'username' => $username,
  33. 'password' => $password,
  34. 'organisation_id' => $organisation_id,
  35. 'config_id' => $config_id,
  36. );
  37. // Set the API url
  38. if (!$api_url) {
  39. if ($this->debug)
  40. $this->_api_url = 'https://development.voicevault.net/RestApi700';
  41. else
  42. $this->_api_url = null;
  43. } else {
  44. $this->_api_url = $api_url;
  45. }
  46. }
  47. /**
  48. * VoiceVault RegisterClaimant method.
  49. *
  50. * @return SimpleXMLElement result of the request
  51. */
  52. public function RegisterClaimant()
  53. {
  54. $result = $this->doRequest($this->_api_url.'/RegisterClaimant.ashx',
  55. 'POST',
  56. array(
  57. 'username' => $this->_auth['username'],
  58. 'password' => $this->_auth['password'],
  59. 'organisation_unit' => $this->_auth['organisation_id'],
  60. )
  61. );
  62. return new SimpleXMLElement($result);
  63. }
  64. /**
  65. * VoiceVault StartDialogue method.
  66. *
  67. * @param string $claimant_id
  68. * @param string $externalRef External reference that can be used to
  69. * perform a lookup in the system.
  70. * @return SimpleXMLElement result of the request
  71. */
  72. public function StartDialogue($claimant_id, $externalRef=null)
  73. {
  74. $result = $this->doRequest($this->_api_url.'/StartDialogue.ashx',
  75. 'POST',
  76. array(
  77. 'username' => $this->_auth['username'],
  78. 'password' => $this->_auth['password'],
  79. 'configuration_id' => $this->_auth['config_id'],
  80. 'claimant_id' => $claimant_id,
  81. 'external_ref' => $externalRef,
  82. )
  83. );
  84. return new SimpleXMLElement($result);
  85. }
  86. /**
  87. * VoiceVault SubmitPhrase method.
  88. *
  89. * @param string $file File path to the file.
  90. * @param string $phrase Phrase for the recording.
  91. * @param integer $dialogue_id Dialogue ID from StartDialogue.
  92. * @return SimpleXMLElement result
  93. */
  94. public function SubmitPhrase($file, $phrase, $dialogue_id)
  95. {
  96. $result = $this->doRequest($this->_api_url.'/SubmitPhrase.ashx',
  97. // 'http://localhost/sendshorty/index.php/VoiceVault/default/test',
  98. 'POST',
  99. array(
  100. 'username' => $this->_auth['username'],
  101. 'password' => $this->_auth['password'],
  102. 'dialogue_id' => $dialogue_id,
  103. 'prompt' => $phrase,
  104. 'format' => 'Unknown',
  105. 'utterance' => "@$file"
  106. )
  107. );
  108. return new SimpleXMLElement($result);
  109. }
  110. public function GetDialogueSummary($dialogue_id)
  111. {
  112. $result = $this->doRequest($this->_api_url.'/GetDialogueSummary.ashx',
  113. 'POST',
  114. array(
  115. 'username' => $this->_auth['username'],
  116. 'password' => $this->_auth['password'],
  117. 'dialogue_id' => $dialogue_id,
  118. )
  119. );
  120. return new SimpleXMLElement($result);
  121. }
  122. /**
  123. * Initates a curl request.
  124. */
  125. private function doRequest($url, $method='GET', $parameters=array())
  126. {
  127. $ch = curl_init($url);
  128. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  129. if ($method == 'POST') {
  130. curl_setopt($ch, CURLOPT_POST, 1);
  131. }
  132. if (!empty($parameters)) {
  133. curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
  134. }
  135. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  136. return curl_exec($ch);
  137. }
  138. }