<?php

$apiKey = 'AIzaSyDPQcTZu5Ux-xKpJkP1nD2r9HyfVzz73uY';
$text = 'Hello, this is a test message.';

$url = 'https://texttospeech.googleapis.com/v1/text:synthesize?key=' . $apiKey;

$data = array(
'input' => array(
'text' => $text
),
'voice' => array(
'languageCode' => 'en-US',
'name' => 'en-US-Standard-B'
),
'audioConfig' => array(
'audioEncoding' => 'MP3'
)
);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);

$a = json_decode($response);

if (curl_errno($ch)) {
echo 'Error: ' . curl_error($ch);
} else {
// Handle the response, e.g., save the audio data to a file
file_put_contents('output.mp3', $a->audioContent);
}

curl_close($ch);

?>