E
E
Eugene2017-07-23 17:47:40
Google
Eugene, 2017-07-23 17:47:40

Free Speech API from Google?

Hello, the thing is, you need to recognize the text using Google, but Google Speech was made paid and it doesn’t fit, I tried to pull out api_key from Google translator, it seems to work, but there is a problem that I don’t always send, I get {"result":[]}
I found the code on open spaces of the Internet, who will tell you what's wrong?

<?php
// Используем cURL для формирования HTTP POST-запроса к Google API
// AIzaSyAqk7vE0vQDR3JItUPgFp6bcPqgJz8h8tI
// AIzaSyBOti4mM-6x9WDnZIjIeyEU21OpBXqWBgw
//AIzaSyCPYK77NqpUfWoHof_HlhnKaGZAsaMT02Q

/* Если возникает ошибка
URL error number:60
URL error:SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
Можно либо подсунуть найденный в просторах Интернета давно устаревший сертификат curl-ca-bundle.crt, либо просто отключить проверку
*/

/* Format *.flac
Channels : 2
Sample Rate : 44100
Precision : 32-bit
Sample Encoding: 32-bit Float
*/

$file_to_upload = array('myfile'=>'@'.'send.flac');

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/speech-api/v2/recognize?output=json&lang=ru-RU&key=AIzaSyBOti4mM-6x9WDnZIjIeyEU21OpBXqWBgw&client=chromium&maxresults=6&pfilter=2');

// curl_setopt($ch, CURLOPT_CAINFO, 'd:\server\apps\curl-ca-bundle.crt');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);  // Отключить ошибку "SSL certificate problem, verify that the CA cert is OK"
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);  // Отключить ошибку "SSL: certificate subject name 'hostname.ru' does not match target host name '123.123.123.123'"
curl_setopt($ch, CURLOPT_POST,1);
//curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: audio/l16; rate=16000;'));  // для wav файлов, но по слухам ни у кого не работает
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: audio/x-flac; rate=44100;'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $file_to_upload);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result=curl_exec ($ch);

// показать инфу и ошибки
print_r(curl_getinfo($ch)); 
echo '<br>URL error number:' . curl_errno($ch); 
echo '<br>URL error:' . curl_error($ch);
echo '<br>';

curl_close ($ch);

// Покачать весь ответ гугла
echo '<br>result='.$result;
echo "<br>";

// По не выясненным пока причинам, в версии V2 первый ответ всегда пустой. Берем второй.
$result_arr = split("\n", $result); $result = $result_arr[1];
// Google возвращает JSON, поэтому парсим стандартной функцией. Доступна в PHP 5.2
$json_array = json_decode($result, true);
$voice_cmd = $json_array['result'][0]['alternative'][0]['transcript'];
$confidence = $json_array['result'][0]['alternative'][0]['confidence'];
echo('<br>voice_cmd='.$voice_cmd);
echo('<br>confidence='.$confidence);

?>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dimonchik, 2017-07-23
@dimonchik2013

https://freelansim.ru/

J
Jeffrey, 2017-07-24
@Hardch

As of PHP 5.5.0, the @ prefix is ​​deprecated and files can be uploaded using CURLFile.
php.net/manual/ru/function.curl-setopt.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question