34 lines
1.0 KiB
PHP
34 lines
1.0 KiB
PHP
<?php
|
|
// api/tts.php
|
|
header('Content-Type: audio/mpeg');
|
|
header('Cache-Control: public, max-age=31536000');
|
|
|
|
$text = isset($_GET['text']) ? trim($_GET['text']) : '';
|
|
$lang = isset($_GET['lang']) ? trim($_GET['lang']) : 'en';
|
|
|
|
if (empty($text)) {
|
|
http_response_code(400);
|
|
exit;
|
|
}
|
|
|
|
$url = 'https://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&tl=' . urlencode($lang) . '&q=' . urlencode($text);
|
|
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
|
'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
|
'Referer: https://translate.google.com/'
|
|
]);
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
|
$audioData = curl_exec($ch);
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
curl_close($ch);
|
|
|
|
if ($httpCode === 200 && $audioData) {
|
|
echo $audioData;
|
|
} else {
|
|
http_response_code(500);
|
|
}
|