Thursday, June 26, 2014

SpokenData API - File upload

SpokenData API allows developers to easily add new recordings to their media library. You can either pass a media file URL or you can upload the whole file using the HTTP PUT method that was recently implemented into SpokenData. This post will demonstrate the second option - uploading a file through the SpokenData API call.

So, how does it work? Each SpokenData API function is composed of SpokenData base API url, USER-ID, API-TOKEN and the name of the function.

SpokenData API url:
USER-ID: different for each user, available for signed-in at http://spokendata/api
API-TOKEN: different for each user, available for signed-in at http://spokendata/api
API function: recording/put

If we concatenate the above values, we get the API call url. It may look like this:

Each API call can have parameters. When uploading a new file through API, you need to enter the recording filename and the language for automatic speech processing. So basically, you will end up with a URL looking like this:

Basically, there are also available other parameters you can read about in the SpokenData API documentation. When you call the above url, don't forget to put the file content.

Here is a code example that uploads an MP3 file using the HTTP PUT method to SpokenData.
$fileToUpload = 'd:/audio.mp3';
$url = '';

$file = fopen($fileToUpload, "rb");
$curl = curl_init();
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_VERBOSE, '1');
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect: '));
curl_setopt($curl, CURLOPT_PUT, 1);
curl_setopt($curl, CURLOPT_INFILE, $file);
curl_setopt($curl, CURLOPT_INFILESIZE, filesize($fileToUpload));

$response = curl_exec($curl);
header ("Content-Type:text/xml");
echo $response;

The server responds in XML. Here is an example response of the above script.
<?xml version="1.0" encoding="utf8"?>
<message>New media file &quot;audio.mp3&quot; was successfully added.</message>
<recording id="1373"></recording>

As you can see, the file audio.mp3 was successfully added and assigned the recording id = 1373.

No comments:

Post a Comment