简体   繁体   English

通过PHP / cURL发送Base64数据会导致语音转文本仅返回一个空的json字符串?

[英]Sending Base64 data by PHP / cURL results in speech-to-text only returning an empty json string?

I'm trying to send a Base64 string to Google's Speech-to-Text API in PHP. 我正在尝试将Base64字符串发送到PHP中的Google语音到文本API。 For annoying reasons I can't use composer to get the PHP library, so I'm trying to use the Speech to Text API through cURL. 由于烦人的原因,我无法使用composer来获取PHP库,因此我试图通过cURL使用Speech to Text API。

$file = base64_encode( file_get_contents(__DIR__.'/test.ogg') );
$data = array(
  'config' => array(
    "encoding" => 'OGG_OPUS',
    'sampleRateHertz' => 16000,
    "languageCode"=> "en-GB"
  ),
  "audio"=> array (
    "content"=> $file
  )
);

$ch = curl_init("https://speech.googleapis.com/v1/speech:recognize?key=AIzaSyBxxxxxxxxxxxxxxxxxxxx-xxxxxxxxxxx");

curl_setopt_array($ch, array(
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => json_encode($data),
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT => 10,
    CURLOPT_HTTPHEADER => array(
        "Content-Type: application/json"
    )
));
$json = curl_exec($ch);

What I know: 我知道的:

  • The test.ogg is a valid Ogg/Opus file. test.ogg是有效的Ogg / Opus文件。
  • The API key is correct API密钥正确
  • Using a sample file in audio->uri works fine. 在audio-> uri中使用示例文件可以正常工作。

So, a simple question, I hope - what am I doing wrong? 所以,我希望有一个简单的问题-我做错了什么?

I took your exact code and ran it myself with PHP 7.1, and I got an invalid key error back from the API. 我获取了您的确切代码,并使用PHP 7.1亲自运行了该代码,并且从API返回了无效的密钥错误。 This suggests that the code itself is working fine and that it is related to your system, your network or some configurations. 这表明该代码本身可以正常工作,并且与您的系统,网络或某些配置有关。

Some suggestions of what you could look into; 关于您可以研究的内容的一些建议;

  • Do you have the curl extension installed and configured correctly? 是否已正确安装和配置curl扩展?
  • Can you access the API outside of PHP, for example via Paw or Postman ? 您可以通过PawPostman访问PHP之外的API吗?
  • Have you tried testing this code on another machine, and on another network? 您是否尝试过在另一台计算机和另一个网络上测试此代码?

Okay. 好的。 Turns out that the Google API is really picky about Ogg Opus files. 原来Google API对于Ogg Opus文件确实很挑剔。 Running the file through ffmpeg to convert it to flac sorts it out. 通过ffmpeg运行文件以将其转换为flac可以对其进行排序。

Which is all a bit odd. 有点奇怪。 Chrome (Google) creates a webm (Google) opus file which the Speech-to-text system (Google) can't handle. Chrome(Google)创建一个语音转文本系统(Google)无法处理的webm(Google)作品文件。 Convert it to the Ogg Opus file the API claims it can handle, and it fails silently. 将其转换为API声称可以处理的Ogg Opus文件,并且它会以静默方式失败。 Convert that Ogg Opus to a flac file (so, from lossy to lossless, which is mental) and it suddenly works. 将该Ogg Opus转换为flac文件(因此,从有损变为无损,这是很重要的),它突然起作用了。 And works incredibly well. 并且效果非常好。

Or anyone else stumbling on this in the future, the ffmpeg command was 或其他将来绊脚石的人,ffmpeg命令是

ffmpeg -i input.ogg -b:a 16000 output.flac

That sets it to 16000 bps, but I can't for the life of me remember what the :a does. 它将其设置为16000 bps,但是我一生都无法记住:a的作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM