You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel 8集成Google Cloud Speech to Text API出现未定义变量与类报错

问题1:Undefined variable $speechClient报错解决

  • 首先在控制器头部的导入区域新增SpeechClient的导入语句:
use Google\Cloud\Speech\V1\SpeechClient;
  • 在transcribedText方法中调用recognizeAudioStream之前,先实例化SpeechClient对象,需要传入你从谷歌云平台下载的服务账号JSON密钥配置:
// 请先将谷歌云服务账号密钥文件上传到storage/app目录下
$speechClient = new SpeechClient([
    'credentials' => storage_path('app/google-service-account.json')
]);

问题2:Intelephense提示未定义类型解决

  • 先确认扩展包是通过composer正确安装的,Laravel Sail环境请执行以下命令安装:
sail composer require google/cloud-speech
  • 执行composer自动加载文件重建命令:
# 本地环境
composer dump-autoload
# Sail环境
sail composer dump-autoload
  • 重启IDE并重建项目索引:VSCode按Ctrl+Shift+P选择Reload Window即可,PHPStorm选择File > Invalidate Caches...后重启IDE即可。

补充修复隐藏参数错误

你当前代码中传给recognizeAudioStream的第二个参数是文件内容字符串,不符合接口要求的资源流类型,需要修改为:

// 替换原有的$file = Storage::disk('public')->get('test.flac');
$audioResource = fopen(Storage::disk('public')->path('test.flac'), 'r');
$responses = $speechClient->recognizeAudioStream($config, $audioResource);

内容的提问来源于stack exchange,提问作者LaFllamme

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 19:06:03