首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法使用PHP从webapp访问云语音API

无法使用PHP从webapp访问云语音API
EN

Stack Overflow用户
提问于 2020-05-26 16:31:43
回答 1查看 99关注 0票数 1

我正在尝试转录用户从计算引擎实例上运行的webapp上传的音频文件,该实例可以访问使用composer安装的所有云API和客户端库。然而,在尝试使用PHP发送我的请求后,我没有从云语音API获得任何响应。谁能帮我弄清楚什么是am missing.Here是我的PHP代码,音频文件被定向到一个带有文件选择器的表单。我也试着从云存储中转录音频文件,但仍然没有得到任何东西

代码语言:javascript
运行
复制
<?php
require __DIR__ . '/vendor/autoload.php';
use Google\Cloud\Speech\V1\SpeechClient;
use Google\Cloud\Speech\V1\RecognitionAudio;
use Google\Cloud\Speech\V1\RecognitionConfig;
use Google\Cloud\Speech\V1\RecognitionConfig\AudioEncoding;

$audioFile = $_FILES["my_audio"]; //from file picker
$encoding = AudioEncoding::LINEAR16;
$sampleRateHertz = 32000;
$languageCode = 'en-US';

$content = file_get_contents($audioFile);
$audio = (new RecognitionAudio())
   ->setContent($content);
$config = (new RecognitionConfig())
   ->setEncoding($encoding)
   ->setSampleRateHertz($sampleRateHertz)
   ->setLanguageCode($languageCode);

$client = new SpeechClient();
$operation = $client->longRunningRecognize($config, $audio);
$operation->pollUntilComplete();

if ($operation->operationSucceeded()) {
   $response = $operation->getResult();

// each result is for a consecutive portion of the audio. iterate
// through them to get the transcripts for the entire audio file.
    foreach ($response->getResults() as $result) {
        $alternatives = $result->getAlternatives();
        $mostLikely = $alternatives[0];
        $transcript = $mostLikely->getTranscript();
        $confidence = $mostLikely->getConfidence();
        printf('Transcript: %s' . PHP_EOL, $transcript);
        printf('Confidence: %s' . PHP_EOL, $confidence);
    }
} else {
    print_r($operation->getError());
}

$client->close();
?>
EN

回答 1

Stack Overflow用户

发布于 2020-07-11 08:43:29

$_FILES["my_audio"]中没有绝对路径。既然你提到文件已经上传了,我想知道你是不是设置了这样的东西:

代码语言:javascript
运行
复制
$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

另一方面,如果您没有收到来自Speech API服务的任何响应,这对我来说可能意味着请求没有启动/触发。您可以在file upload期间执行验证,或者使用$_FILES["pictures"]["error"]检查错误,以查看是否一切正常。

如果问题仍然存在,即使使用$uri = 'gs://your-bucket-name/your-audio-file'的全球控制系统,问题可能不是代码,在这种情况下,请详细说明您的用例中涉及的其他组件。你提到了作曲家,我想知道你是不是指云作曲家。

更新

我不是PHP专家,但我用this example解决了这个问题

在比较了你的文件之后,我发现你有:

代码语言:javascript
运行
复制
$uri = "gs://speech-text-audio/h_ana1.flac";
$content = file_get_contents($uri);
$audio = (new RecognitionAudio())
    ->setContent($content);

当它对我起作用的时候:

代码语言:javascript
运行
复制
$uri = 'gs://cloud-samples-tests/speech/brooklyn.flac';
$audio = (new RecognitionAudio())
    ->setUri($uri);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62017773

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档