首页 > 解决方案 > 运行谷歌文本到语音 api 文档页面上给出的 php 脚本会出现错误“无法创建资源”

问题描述

我正在运行 text-to-api 页面的文档页面上提供的 php 代码。每跑8-10次就成功了。它给了我 Error creating resource: [message] fopen(): php_network_getaddresses: getaddrinfo failed: Name or service not known [file] /Desktop/php_projects/text_speech/vendor/guzzlehttp/guzzle/src/Handler/StreamHandler.php [line] 323 [message] fopen(https://oauth2.googleapis.com/token): failed to open stream: php_network_getaddresses: getaddrinfo failed: Name or service not known [file] /Desktop/php_projects/text_speech/vendor/guzzlehttp/guzzle/src/Handler/StreamHandler.php [line] 323

它在这条线上给了我错误


$response = $client->synthesizeSpeech($synthesisInputText, $voice, $audioConfig);
require __DIR__ . '/vendor/autoload.php';
// Imports the Cloud Client Library
use Google\Cloud\TextToSpeech\V1\AudioConfig;
use Google\Cloud\TextToSpeech\V1\AudioEncoding;
use Google\Cloud\TextToSpeech\V1\SsmlVoiceGender;
use Google\Cloud\TextToSpeech\V1\SynthesisInput;
use Google\Cloud\TextToSpeech\V1\TextToSpeechClient;
use Google\Cloud\TextToSpeech\V1\VoiceSelectionParams;

$client = new TextToSpeechClient([
    'credentials' => ('/Documents/gcp_credentials/config.json')
]);

$synthesisInputText = (new SynthesisInput())
    ->setText('Hello, world! This is just for testing purpose.Lorem Ipsum is simply dummy text of the printing and typesetting industry.This is just for testing purpose.Lorem Ipsum is simply dummy text of the printing and typesetting industry');

$voice = (new VoiceSelectionParams())
    ->setLanguageCode('en-IN')
    ->setName('en-IN-Wavenet-A')
    ->setSsmlGender(SsmlVoiceGender::FEMALE);

$effectsProfileId = "telephony-class-application";

$audioConfig = (new AudioConfig())
    ->setAudioEncoding(AudioEncoding::LINEAR16)
    ->setEffectsProfileId(array($effectsProfileId));

$response = $client->synthesizeSpeech($synthesisInputText, $voice, $audioConfig);
$audioContent = $response->getAudioContent();

file_put_contents('output.wav', $audioContent);

每次运行此代码时,我都想无错误地输出结果。

标签: phpcurlgoogle-cloud-platformgoogle-text-to-speechguzzle

解决方案


推荐阅读