Php 验证并使用谷歌云语音API
我正在尝试制作一个PHP应用程序,使用它我可以向google语音服务发送一个.flac文件,并获取返回的文本 遵循 这是身份验证代码:Php 验证并使用谷歌云语音API,php,google-cloud-platform,google-cloud-speech,Php,Google Cloud Platform,Google Cloud Speech,我正在尝试制作一个PHP应用程序,使用它我可以向google语音服务发送一个.flac文件,并获取返回的文本 遵循 这是身份验证代码: require 'vendor/autoload.php'; use Google\Cloud\Core\ServiceBuilder; // Authenticate using keyfile data $cloud = new ServiceBuilder([ 'keyFile' => json_decode(file_get_conten
require 'vendor/autoload.php';
use Google\Cloud\Core\ServiceBuilder;
// Authenticate using keyfile data
$cloud = new ServiceBuilder([
'keyFile' => json_decode(file_get_contents('b.json'), true)
]);
这是语音代码:
use Google\Cloud\Speech\SpeechClient;
$speech = new SpeechClient([
'languageCode' => 'en-US'
]);
// Recognize the speech in an audio file.
$results = $speech->recognize(
fopen(__DIR__ . '/audio_sample.flac', 'r')
);
foreach ($results as $result) {
echo $result->topAlternative()['transcript'] . "\n";
}
当然,$cloud变量从未使用过。它应该去哪里
我跑了,但还是跑了
未捕获异常“Google\Cloud\Core\exception\ServiceException”
消息{错误:{代码:401,消息:请求已被删除
身份验证凭据无效。应为OAuth 2访问令牌,
登录cookie或其他有效的身份验证凭据。请参阅
.,
状态:未经身份验证}
我只想做一个简单的查询。任何帮助都将不胜感激。试试以下方法:
use Google\Cloud\Speech\SpeechClient;
$speech = new SpeechClient([
'languageCode' => 'en-US',
'keyFile' => json_decode(file_get_contents('b.json'), true)
]);
// Recognize the speech in an audio file.
$results = $speech->recognize(
fopen(__DIR__ . '/audio_sample.flac', 'r')
);
foreach ($results as $result) {
echo $result->topAlternative()['transcript'] . "\n";
}
关于文档,您提出了一个很好的观点。过去几个月的变化造成了这种令人困惑的局面,值得再看一眼来澄清。我已经创建了一个解决方案
ServiceBuilder是一个提供工厂的类,允许您一次性配置Google Cloud PHP,并创建不同的客户端,如继承该配置的语音或数据存储。ServiceBuilder::_构造上的所有选项在客户端本身中也可用,例如SpeechClient,但有几个例外
如果要使用ServiceBuilder,可以执行以下操作:
use Google\Cloud\Core\ServiceBuilder;
$cloud = new ServiceBuilder([
'keyFile' => json_decode(file_get_contents('b.json'), true)
]);
$speech = $cloud->speech([
'languageCode' => 'en-us'
]);
或者,您可以在使用api之前调用putenv:
/** Setting Up Authentication. */
$key_path = '/full/path/to/key-file.json';
putenv( 'GOOGLE_APPLICATION_CREDENTIALS=' . $key_path );
是否创建了密钥文件?@rtfm:Yes,b.json是有效的密钥文件。已按照以下链接创建它: