I have a php file in which I am trying to make a function where I pass in text for the Google cloud API to return me a text to speech file.
text-to-speech.php
<?php
require_once __DIR__ . '/vendor/autoload.php';
class TextToSpeech
{
public function TextToSpeech(array $input)
{
$apiKey = 'mykey';
$text = $input;
$languageCode = 'lv-LV';
$audioEncoding = GoogleCloudTextToSpeechV1AudioEncoding::MP3;
try {
$client = new GoogleCloudTextToSpeechV1TextToSpeechClient(['credentials' => $apiKey]);
$synthesisInput = (new GoogleCloudTextToSpeechV1SynthesisInput())
->setText($text);
$voice = (new GoogleCloudTextToSpeechV1VoiceSelectionParams())
->setLanguageCode($languageCode)
->setSsmlGender(GoogleCloudTextToSpeechV1SsmlVoiceGender::FEMALE);
$audioConfig = (new GoogleCloudTextToSpeechV1AudioConfig())
->setAudioEncoding($audioEncoding);
$response = $client->synthesizeSpeech($synthesisInput, $voice, $audioConfig);
file_put_contents('output.mp3', $response->getAudioContent());
echo 'Audio file saved successfully.';
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}
}
}
?>
Front-page.php
...
$test = new TextToSpeech;
$test->TextToSpeech(["Hello my name is BingBong"]);
...
composer.json
...
"google/cloud" : "^0.229",
...
Error: Could not find keyfile: mykey
This is the only error I have not been able to fix. Where I have written mykey is my actual API key and I removed all the possible restrictions I could find on their website, but it still doesn’t want to work
I tried other ways of passing my API key in the hopes that it would recognise it, but it didn’t. This is my first time working with an API, so I don’t really know how else I could fix this issue.