<?php

// Copyright 2012 Nexiwave Canada. All rights reserved.
// Nexiwave Canada PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.

if (sizeof($argv) != 4) {
    error_log("Usage: $argv[0] <username> <password> <audio_file>");
    exit(1);
}
else {
    $user = $argv[1];
    $pass = $argv[2];
    $file = $argv[3];

    // Post this audio file to Nexiwave to transcribe
    $url = 'https://api.nexiwave.com/transcribe';

    // To receive transcript as plain text, instead of html format, comment this line out (for SMS, for example)
    //$url = $url . '&transcriptFormat=html';


    // Prepare curl
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_USERPWD, "$user:$pass");

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Accept: text/raw-transcript',        
        ));
    curl_setopt($ch, CURLOPT_VERBOSE, 0);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    # curl_setopt($ch, CURLOPT_MAXREDIRS, 100);

    // To explicitly configure to wait indefintely for sync requests:
    #$timeout = 0;
    #curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    #curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    #set_time_limit($timeout);      // time execution of php script self

    $post = array("data.mediaFileData"=>sprintf("@%s", $file),);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

    // Ready to send:
    $result = curl_exec($ch);
    $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    switch($code) {
    case 200:             
           echo $result;
           break;
    default:
           throw new exception($result);
    }
}
?>