PHP Speech to Text using the Google Speech API

August 6th 2013

Using the Google Speech API is great. It gives the ability to send a flac file to Google's servers and they'll convert it into the 'best guess' of text.

So far, it's been right on the money. Anyway, I published the code up on GitHub. You can find it here: https://github.com/rogerthomas84/php-speech-to-text

Before you hop in, I have to give credit where credit is due. The hard work was done by Mike Pultz (View his post)

Here's the overview:

PHP Code:
<?php
include 'GoogleSpeechToText.php';

// Your API Key goes here.
$apiKey '';
$speech = new GoogleSpeechToText($apiKey);
$file realpath(__DIR__ '/quick.flac'); // Full path to the file.
$bitRate 44100// The bit rate of the file.
$result $speech->process($file$bitRate'en-US');
var_dump($result);

Running that code (with the flac file from my GitHub project) outputs:

Text Snippet:
array(1) {
  [0]=>
  array(2) {
    ["alternative"]=>
    array(5) {
      [0]=>
      array(2) {
        ["transcript"]=>
        string(44) "the quick brown fox jumped over the lazy dog"
        ["confidence"]=>
        float(0.87096781)
      }
      [1]=>
      array(1) {
        ["transcript"]=>
        string(43) "the quick brown fox jumps over the lazy dog"
      }
      [2]=>
      array(1) {
        ["transcript"]=>
        string(49) "the quick brown fox jumped over the lazy dog cafe"
      }
      [3]=>
      array(1) {
        ["transcript"]=>
        string(49) "the quick brown fox jumped over the lazy dog food"
      }
      [4]=>
      array(1) {
        ["transcript"]=>
        string(47) "the quick brown fox jumped over the lazy dog hi"
      }
    }
    ["final"]=>
    bool(true)
  }
}

asmi shah commented on Dec 30th 2014

Hi Roger Thomas,

Thanks for the code. I exactly followed the steps and finally if I try to run your example code, it says "boolean false".

I created a project on google developers console and enabled the speechAPI and added the key to the php file. The rest is the same then. How could i check if the API is working correctly ?

Something goes wrong here I guess :
$res = curl_multi_getcontent($this->downloadHandle);

as "$this->downloadHandle" echoes "resource(4, curl)" but "$res" echoes a null string ''.

Any help is appreciated! Thanks already!

ritesh commented on Dec 31st 2014

I have been trying to run this program. Generated api key and using it.

But the output of var_dump is always bool(false).

Any help will be highly appreciated.
Thanks

federico commented on Jan 7th 2016

Thanks for sharing excellent was looking for this very reason, my question is what generates the apikey according to the following post:
https://www.chromium.org/developers/how-tos/api-keys

as you indicated, when generating the keys at the end left me with two keys but which are not used as probe with both and I do not work, I'm missing something ??

excuse my bad English, I just write in Spanish

happy New Year

TaiCheng commented on Nov 14th 2016

Hello Roger,

Thanks for your php code for google speech api.
I am happy now to trying to use this and I am wondering why I have empty result when I use it with my own API key.
Please help me.

Regards,
From Zhao