Created
April 14, 2012 22:19
-
-
Save Opus1no2/2388126 to your computer and use it in GitHub Desktop.
Convenience class for Ifbyphone audio API
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 | |
/** | |
* | |
* List all recordings | |
*/ | |
try { | |
$test = new Ibp_Audio(); | |
$test->loadRecordings('27431'); | |
$test->getRecordings(); | |
} catch (Exception $e) { | |
echo "Caught Exception: ", $e->getMessage() . PHP_EOL; | |
} | |
/** | |
* | |
* Download all recording | |
*/ | |
try { | |
$test = new Ibp_Audio(); | |
$test->loadRecordings('27431'); | |
$test->download('mp3', 'all'); | |
} catch (Exception $e) { | |
echo "Caught Exception: ", $e->getMessage() . PHP_EOL; | |
} | |
/** | |
* | |
* Download a single recordings | |
*/ | |
try { | |
$test = new Ibp_Audio(); | |
$test->loadRecordings('27431'); | |
$test->download('mp3', null, '1008027652920023'); | |
} catch (Exception $e) { | |
echo "Caught Exception: ", $e->getMessage() . PHP_EOL; | |
} | |
/** | |
* | |
* Upload a recording | |
*/ | |
try { | |
$test = new Ibp_Audio(); | |
$test->upload('general', 'http://travisscotttillotson.com/IBP/Q1.wav'); | |
} catch (Exception $e) { | |
echo "Caught Exception: ", $e->getMessage() . PHP_EOL; | |
} | |
/** | |
* | |
* Remove a recording | |
*/ | |
try { | |
$test = new Ibp_Audio(); | |
$test->remove('1007307652802238'); | |
} catch (Exception $e) { | |
echo "Caught Exception: ", $e->getMessage() . PHP_EOL; | |
} |
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 | |
/** | |
* | |
* Convenience class for using Ifbyphone recording API | |
* | |
* This class provides conveniant methods for using Ifbyphone | |
* recording API and can be expanded to include intelligent | |
* error handling based on the xml response from Ibyphone for | |
* any API call. | |
* | |
* @TODO - include methods for downloading recordings for SurVo and email. | |
*/ | |
class Ibp_Audio | |
{ | |
const IBP_BASE = 'https://secure.ifbyphone.com/ibp_api.php?'; | |
const IBP_KEY = 'YOUR IFBYPHONE API KEY'; | |
private $fh; | |
private $curl; | |
private $option; | |
private $xmllist; | |
public function __construct() | |
{ | |
$this->curl = curl_init(); | |
$this->option = array( | |
'api_key' => self::IBP_KEY, | |
'path' => $this->upload_dir | |
); | |
} | |
/** | |
* | |
* Load recordings as an XML object. Recordings must | |
* be loaded before they can be downloaded or listed. | |
* | |
* @param string $id | |
*/ | |
public function loadRecordings($id) | |
{ | |
$this->option['id'] = $id; | |
$this->option['type'] = 'findme'; | |
$this->option['action'] = 'recording.list'; | |
$raw_list = $this->__request(); | |
$this->xmlist = new SimpleXMLElement($raw_list); | |
} | |
public function getRecordings() | |
{ | |
return $this->xmlist; | |
} | |
/** | |
* | |
* Upload an audio file of a .wav format to a directory | |
* in an Ifbyphone account. The $type is a unique ID and only | |
* necessary for 'survo' and 'vmail'. The $directory must be an | |
* accessable file path to the audio file to be uploaded. | |
* Valid types include: | |
* | |
* general - General audio for SurVo custom audio prompts | |
* survo - SurVo prompts | |
* voting - Prompts for VoteByPhone | |
* holdmusic - Hold music for Find Me | |
* vmail - Voice mail greetings | |
* whisperaudio - Whisper audio for Find Me | |
* prompts - Advanced Audio prompts for Find Me | |
* | |
* @param string $type | |
* @param string $directory | |
* @param string $id | |
*/ | |
public function upload($type, $directory, $id = null) | |
{ | |
$this->option['id'] = $id; | |
$this->option['type'] = $type; | |
$this->option['path'] = $directory; | |
$this->option['action'] = 'recording.upload'; | |
$this->__request(); | |
} | |
/** | |
* | |
* Download a single file or all files for findme or api call types. | |
* Files can be downloaded in either a wav or mp3 format. | |
* | |
* @param string $format | |
* @param mixed $all | |
* @param string $sid | |
*/ | |
public function download($format, $all = null, $sid = null) | |
{ | |
$this->option['id'] = $id; | |
$this->option['type'] = 'findme'; | |
$this->option['format'] = $format; | |
$this->option['action'] = 'recording.download'; | |
$recordings = $this->getRecordings(); | |
if ($format != 'wav' && $format != 'mp3') { | |
throw new Exception('Invalid file format'); | |
} else { | |
if (isset($sid) && isset($all)) { | |
throw new Exception('You cannot specify both "$sid" and "$all"'); | |
} | |
if (isset($sid) && !isset($all)) { | |
$this->option['sid'] = $sid; | |
foreach ($recordings->data->recording as $value) { | |
if ($value->sid == $sid) { | |
$path = array( | |
'sid' => $value->sid, | |
'dnis' => $value->dnis, | |
'trans_num' => $value->number_connected, | |
'format' => $format | |
); | |
$this->fh = @fopen($this->format($path), 'w'); | |
$this->__request($this->fh); | |
} | |
} | |
} else { | |
if (isset($all) && !isset($sid)) { | |
foreach ($recordings->data->recording as $value) { | |
$this->option['sid'] = (string)$value->sid; | |
$path = array( | |
'sid' => $value->sid, | |
'dnis' => $value->dnis, | |
'trans_num' => $value->number_connected, | |
'format' => $format | |
); | |
$this->fh = @fopen($this->format($path), 'w'); | |
$this->__request($this->fh); | |
} | |
} | |
} | |
} | |
} | |
/** | |
* | |
* Format the files to to download | |
* | |
* @param array $format | |
*/ | |
public function format(array $format) | |
{ | |
$path = '/path/to/download/directory'; | |
$path .= $format['sid']; | |
$path .= "-" . $format['dnis']; | |
$path .= "-" . $format['trans_num']; | |
$path .= "." . $format['format']; | |
return $path; | |
} | |
/** | |
* | |
* Remove a recording from an account. The $now param | |
* can be set to 1 to delete a file immediately. If the | |
* file is not set to delete immediately Ifbyphone will | |
* delete the file in xx days. | |
* | |
* @param string $sid | |
* @param int $now | |
*/ | |
public function remove($sid, $now = null) | |
{ | |
$this->option['sid'] = $sid; | |
$this->option['action'] = 'recording.remove'; | |
if (isset($now) && $now != 1) { | |
throw new Exception('The value for now must be 1'); | |
} elseif (isset($now) && $now == 1) { | |
$this->option['delete_now'] = $now; | |
} | |
$this->__request(); | |
} | |
/** | |
* | |
* Set options for for cURL request. Both a timeout and a sleep | |
* time of 30 seconds have been added as an example of best practice | |
* | |
* @param resource $fh | |
*/ | |
public function __request($fh = null) | |
{ | |
curl_setopt($this->curl, CURLOPT_URL, $this->url()); | |
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($this->curl, CURLOPT_TIMEOUT, 120); | |
if (isset($this->fh)) { | |
curl_setopt($this->curl, CURLOPT_FILE, $this->fh); | |
} | |
$result = curl_exec($this->curl); | |
sleep(30); | |
return $result; | |
} | |
/** | |
* | |
* Parses the XML response returned from Ifbyphone for a result | |
* of 'failure'. This can augmented to log results or initiate | |
* actions based on the outcome of any give API call. | |
* | |
* @param string $result | |
*/ | |
public function parseResult($result) | |
{ | |
$xml = new SimpleXMLElement($result); | |
foreach ($xml->result as $v) { | |
if ($xml->result == "failed") { | |
//Log something | |
} else { | |
//Log something | |
} | |
} | |
} | |
/** | |
* | |
* Get HTTP query for web request | |
*/ | |
public function getQuery() | |
{ | |
$query = http_build_query($this->option); | |
return $query; | |
} | |
/** | |
* | |
* Set URL of web request | |
*/ | |
public function url() | |
{ | |
$url = self::IBP_BASE . $this->getQuery(); | |
return $url; | |
} | |
/** | |
* | |
* Ensure that both the file handle and cURL session | |
* are both closed if they are set when the object is destroyed. | |
*/ | |
public function __destruct() | |
{ | |
@fclose($this->fh); | |
@curl_close($this->curl); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment