Reputation: 949
I have tried everything I know to get the Google Speech to text API to return anything but false but to no avail. I made sure my flac file was a single channel file (ffmpeg -i test.flac -ac 1 test1.flac) - still nothing. What am I doing wrong?
<?php
// Your API Key goes here.
$apiKey = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$audioFile = realpath(__DIR__ . '/test.flac');
$url="https://speech.googleapis.com/v1/speech:recognize?key={$apiKey}";
$audioFileResource = fopen($audioFile, 'r');
$base64Audio = base64_encode(stream_get_contents($audioFileResource));
$settings=array(
'config'=> array(
'encoding'=>'FLAC',
'sampleRateHertz'=>44100,
'maxAlternatives' => 3,
'languageCode' => 'en-US'
),
'audio'=>array(
'content'=>$base64Audio
)
);
$json=json_encode($settings);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $json );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result=curl_exec ($ch);
var_dump($result);
exit;
UPDATE: I have updated the code so that this now works. I had two things wrong. First it was "sampleRateHertz" instead of "sampleRate" in the config settings. Secondly, I had to disable the SSL verification on the peer. This now works.
Returns the following:
D:\>php GoogleSpeechToText_example.php
string(320) "{
"results": [
{
"alternatives": [
{
"transcript": "the quick brown fox jumped over the lazy dog",
"confidence": 0.9850106
},
{
"transcript": "the quick brown fox jumps over the lazy dog",
"confidence": 0.90610087
}
]
}
]
}
Upvotes: 0
Views: 348
Reputation: 949
Just by luck I figured out the problem. I had to add the following line of code:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
Upvotes: 1