Feedback

sonicAPI.com

live demo -

Show docs

parameters

<?php
$accessId = '';
$taskUrl = '';
$parameters = array();
$parameters['access_id'] = $accessId;
$parameters['format'] = 'json';$parameters['format'] = 'mp3-cbr';

// important: the calls require the CURL extension for PHP $ch = curl_init('https://api.sonicAPI.com/' . $taskUrl); curl_setopt($ch, CURLOPT_HEADER, FALSE); curl_setopt($ch, CURLOPT_POST, TRUE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters); // you can remove the following line when using http instead of https, or // you point curl to the CA certificate curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $httpResponse = curl_exec($ch); $infos = curl_getinfo($ch); curl_close($ch); $response = json_decode($httpResponse); if ($infos['http_code'] == 200) { echo "Task succeeded, analysis result:<br />" . json_encode($response); } else { $errorMessages = array_map(function($error) { return $error->message; }, $response->errors); echo 'Task failed, reason: ' . implode('; ', $errorMessages); } if ($infos['http_code'] == 200) { file_put_contents('processing_result.mp3', $httpResponse); echo "Task succeeded, file written to processing_result.mp3"; } else { $responseDom = new DOMDocument(); $responseDom->loadXML($httpResponse); $errorMessages = array(); foreach ($responseDom->getElementsByTagName('errors')->item(0)->childNodes as $error) { $errorMessages[] = $error->attributes->getNamedItem('message')->nodeValue; } echo 'Task failed, reason: ' . implode('; ', $errorMessages); }
<!DOCTYPE html>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" type="application/javascript"></script>
<script type="application/javascript">

var accessId = 'fec166ee-f5b8-46cb-a0b0-a83c1f4c5572';
var taskUrl = '';
var parameters = { blocking: false, format: 'json', access_id: accessId };

// the values for these parameters were taken from the corresponding controls in the demo form
function onTaskStarted(data) { var fileId = data.file.file_id; // request task progress every 500ms var polling = setInterval(pollTaskProgress, 500); function pollTaskProgress() { $.ajax({ url: 'https://api.sonicAPI.com/file/status?file_id=' + fileId + '&access_id=' + accessId + '&format=json', crossDomain: true, success: function(data) { if (data.file.status == 'ready') { onTaskSucceeded(fileId); clearInterval(polling); } else if (data.file.status == 'working') { $('#result').text(data.file.progress + '% done'); } }}); } } function onTaskSucceeded(fileId) { // create HTML5 audio player var downloadUrl = 'https://api.sonicAPI.com/file/download?file_id=' + fileId + '&access_id=' + accessId + '&format=mp3-cbr'; var audio = '<audio src="' + downloadUrl + '" controls="controls" autoplay="autoplay">'; $('#result').html(audio);var downloadUrl = 'https://api.sonicAPI.com/file/download?file_id=' + fileId + '&access_id=' + accessId + '&format=json'; $.ajax({ url: downloadUrl, crossDomain: true, success: function(data) { $('#result').html('Task succeeded, analysis result:<pre>' + JSON.stringify(data, null, 4) + '</pre>'); }}); } function onTaskFailed(response) { var data = $.parseJSON(response.responseText); var errorMessages = data.errors.map(function(error) { return error.message; }); $('#result').text('Task failed, reason: ' + errorMessages.join(',')); } // start task when clicking on the "Start task" button $(document).ready(function() { $('#start').click(function() { // execute an HTTP GET using the task's URL, the parameters and callback functions defined above $.ajax({ url: 'https://api.sonicAPI.com/' + taskUrl, data: parameters, success: onTaskStarted, error: onTaskFailed, crossDomain: true }); }); });
</script> <input type="button" id="start" value="Start task" /> <div id="result" />
Uploading local files using a cross domain request is a bit difficult.
We recommend you try the jQuery Form plugin for your needs.
See the source of the live demo for an example on how to use this plugin.
Choose another input_file to see the JS code example.