Knowledgebase
Prepare recording for download with the API
Posted by Conal Mullan on 30 October 2015 11:37 AM

This php code snippet shows how to invoke the process to prepare a recording for download via the API. 

 

 

// global vars
$username = '[__YOUR_USERNAME__]';
$password = '[__YOUR_PASSWORD__}';
$baseurl = 'https://[__YOUR_BASEURL__]/api/2/';

// wait time before checking download_link_status
// please don't be too aggressive with this
$secondstowait = 60;

// random array of recording ids - generate your list how you want
$recordingsids = array (23145,23037,23265,23266,23267);

// array to hold recording info
$recordingsinfo = array();

function getDownloadLinkStatus($id)

{
$type = 'GET';
$method = 'recording';
$parameters = '/id/'.$id.'/format/php/return/download_link_status';
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $type);
curl_setopt($curl, CURLOPT_URL, $GLOBALS['baseurl'] . '/' . $GLOBALS['username'] . '/' . $method . $parameters);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERPWD, $GLOBALS['username'] . ':' . $GLOBALS['password']);
$result = curl_exec($curl);
curl_close($curl);
eval ('$results = ' . $result . ';');
return $results['download_link_status'];
}

function getDownloadLink($id)
{
$type = 'GET';
$method = 'recording';
$parameters = '/id/'.$id.'/format/php/return/download_link';
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $type);
curl_setopt($curl, CURLOPT_URL, $GLOBALS['baseurl'] . '/' . $GLOBALS['username'] . '/' . $method . $parameters);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERPWD, $GLOBALS['username'] . ':' . $GLOBALS['password']);
$result = curl_exec($curl);
curl_close($curl);
eval ('$results = ' . $result . ';');
return $results['download_link'];
}

function makeOfflineRecording($id)
{
$type = 'GET';
$method = 'recording';
$parameters = '/id/'.$id.'/make_offline_recording/1';
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $type);
curl_setopt($curl, CURLOPT_URL, $GLOBALS['baseurl'] . '/' . $GLOBALS['username'] . '/' . $method . $parameters);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERPWD, $GLOBALS['username'] . ':' . $GLOBALS['password']);
$result = curl_exec($curl);
curl_close($curl);
}

while (count($recordingsinfo) < count($recordingsids))

{
// loop will continue until all processing of downloads has completed
foreach ($recordingsids as &$id)
{
$download_link_status = getDownloadLinkStatus($id);

switch ($download_link_status)
{
case 0:
// download link not ready
// make the api call to generate it
makeOfflineRecording($id);
echo 'sleeping<br>';
sleep($secondstowait);
break;

case 1:
// link being prepared
// lets waits for it
echo 'still sleeping<br>';
sleep($secondstowait);
break;

case 2:
// download link ready
// get download link & prep array
$recordingdata = array ('id'=> $id, 'download_link_status' => $download_link_status, 'download_link' => getDownloadLink($id));
array_push($recordingsinfo,$recordingdata);
break;

case 3:
// download preparation failed
// populate array with error handling info
$recordingdata = array ('id'=> $id, 'download_link_status' => $download_link_status, 'download_link' => 0);
array_push($recordingsinfo,$recordingdata);
break;
}

}

}

print_r($recordingsinfo);
// process ids and download links
// ...
// ..

/*** SAMPLE OUTPUT

sleeping
Array ( [0] => Array ( [id] => 23145 [download_link_status] => 2 [download_link] => https://onsync.digitalsamba.com/storage/recordings/download/23145-3b849120d2ca497027a4b81070d6b8c1.zip ) [1] => Array ( [id] => 23037 [download_link_status] => 2 [download_link] => https://onsync.digitalsamba.com/storage/recordings/download/23037-804eb5dd0e11b4e0d3245a107fbdbbf9.zip ) [2] => Array ( [id] => 23265 [download_link_status] => 2 [download_link] => https://onsync.digitalsamba.com/storage/recordings/download/23265-dd98de966e94866646e9b0d6bca3bffe.zip ) [3] => Array ( [id] => 23266 [download_link_status] => 2 [download_link] => https://onsync.digitalsamba.com/storage/recordings/download/23266-f62d5dc7c6587b0fc33252f8e0e7f54f.zip ) [4] => Array ( [id] => 23145 [download_link_status] => 2 [download_link] => https://onsync.digitalsamba.com/storage/recordings/download/23145-3b849120d2ca497027a4b81070d6b8c1.zip ) [5] => Array ( [id] => 23037 [download_link_status] => 2 [download_link] => https://onsync.digitalsamba.com/storage/recordings/download/23037-804eb5dd0e11b4e0d3245a107fbdbbf9.zip ) [6] => Array ( [id] => 23265 [download_link_status] => 2 [download_link] => https://onsync.digitalsamba.com/storage/recordings/download/23265-dd98de966e94866646e9b0d6bca3bffe.zip ) [7] => Array ( [id] => 23266 [download_link_status] => 2 [download_link] => https://onsync.digitalsamba.com/storage/recordings/download/23266-f62d5dc7c6587b0fc33252f8e0e7f54f.zip ) [8] => Array ( [id] => 23267 [download_link_status] => 2 [download_link] => https://onsync.digitalsamba.com/storage/recordings/download/23267-9d2eb4078138a3a235913af3166e71b0.zip ) )

*/

 

(0 vote(s))
Helpful
Not helpful

Comments (0)
Post a new comment
 
 
Full Name:
Email:
Comments:
CAPTCHA Verification 
 
Please enter the text you see in the image into the textbox below (we use this to prevent automated submissions).

Thank you for choosing OnSync.
For OnSync product information, please go to the Digital Samba website.
www.digitalsamba.com © Digital Samba, SL.