Google Speech to Text API

I was working on a POC around voicemail transcription from unity and I spent longer than needed trying to get this to work the way goods docs say to with all the libraries etc. Then I spent another good chunk of time on github with the same success, but finally got it figured out over the normal way that I have been doing regulr API calls and it works great.

Transcibe Function


function sendToGoogle($file_name) {

    $google['auth_token']  = "<Google API Key>";
    $file_name = fread(fopen($file_name, "r"), filesize($file_name));

    $api_data = array("config"=>array("sampleRate"=>8000

    $options = array(
        'http' => array(
            'header'  => "Content-type: application/json\r\n",
            'method'  => 'POST',
            'content' => json_encode($api_data),

    $context  = stream_context_create($options);
    $result = json_decode(file_get_contents(''
                            .$google['auth_token'], false, $context));

    return $result;

$audio_file = "./file.wav";

//Get google transcription
if ( $transcript = sendToGoogle($audio_file) ) {
    $transcript = $transcript->results[0]->alternatives[0];
    $transcript = "\n\n**Transcript:** (".ceil( $transcript->confidence * 100 )."%)\n".$transcript->transcript;
} else {
    $transcript = "\n\n**Transcript Unavailble**\n";