Created
April 27, 2015 14:26
-
-
Save lemoogle/b56630913d2ed637b88d to your computer and use it in GitHub Desktop.
Speech To Text in PHP with IDOL OnDemand
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
<html> | |
<body> | |
<form action="extract_handler.php" method="post" enctype="multipart/form-data"> | |
<input type="file" name="file" /> | |
<select name="language"> | |
<option>en-US</option> | |
<option>en-GB</option> | |
<option>en-DE</option> | |
<option>en-ES</option> | |
<option>en-FR</option> | |
<option>it-IT</option> | |
<option>zh-CN</option> | |
</select> | |
<input type="submit" value="submit" /> | |
</form> | |
</body> | |
</html> |
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 | |
$url = 'https://api.idolondemand.com/1/api/async/recognizespeech/v1'; | |
$apikey = '<APIKEYHERE>' | |
$output_dir = "."; | |
if(isset($_FILES["file"])) | |
{ | |
$language= $_POST["name"]; // Get language from form | |
$fileName = $_FILES["file"]["name"]; // Filename | |
//move the file to uploads folder | |
move_uploaded_file($_FILES["file"]["tmp_name"],$output_dir.$fileName); | |
//multipart form post using CURL | |
$filePath = realpath($output_dir.$fileName); | |
$post = array('apikey' => $apikey, | |
'language' => $language, | |
'file' => new CurlFile($filePath)); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL,$url); | |
curl_setopt($ch, CURLOPT_POST,1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); | |
$result=curl_exec ($ch); | |
curl_close ($ch); | |
$json = json_decode($result,true); | |
// We now need to fetch the result using the jobID | |
if($json && isset($json['jobID'])) | |
{ | |
$jobID =$json['jobID']; //get the jobid | |
$post = array('apikey' => $apikey); | |
$joburl = "https://api.idolondemand.com/1/job/status/". $jobID; | |
$status="running"; | |
// Loop until the status is set to Finished | |
do { | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL,$joburl); | |
curl_setopt($ch, CURLOPT_POST,1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); | |
$result=curl_exec ($ch); | |
curl_close ($ch); | |
echo "test"; | |
$json = json_decode($result,true); | |
$status=$json["status"]; | |
} while ($status!="finished"); | |
echo $result; | |
} | |
//remove the file | |
unlink($filePath); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment