
This Laravel code shows how to interact/hit to a microservice that uses the Google Text-to-Speech API. The microservice code can be found here: Create Text-to-Speech with Google’s AI Using Python.
While the microservice itself can actually simply hit with CURL, here’s a use case if you use Laravel and want to hit the microservice, I’m sharing this because this is my real use case too, I hope it can help you.
Here is the code:
try {
Log::debug('Creating text to speech...');
// Get the TTS API IP Address from the config
$ttsIPAddress = Config::get('constants.API_TTS_IP_ADDRESS_PRIVATE');
// Define the API endpoint (add the endpoint path if necessary)
$url = $ttsIPAddress . '/convert';
// Initialize cURL session
$ch = curl_init($url);
// Set the cURL options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
// Execute cURL and get the response
$response = curl_exec($ch);
// Check for errors
if ($response === false) {
throw new \Exception('Error during cURL request: ' . curl_error($ch));
}
Log::debug("Genearting success, response: ");
Log::debug($response);
// Close cURL session
curl_close($ch);
} catch (\Exception $e) {
// Optionally log the error or handle it as needed
Log::error('Error hitting text to speech API: ');
Log::error($e->getMessage());
// do nothing or handle the error gracefully
}
Feel free to ask any questions or let me know if you encounter any issues using it.I’ll try to help as much as possible.






Leave a comment