-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tts.php
35 lines (32 loc) · 1.03 KB
/
tts.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$apiKey = 'DEIN_OPENAI_API_SCHLÜSSEL_HIER';
$text = $_POST['text'];
$url = 'https://api.openai.com/v1/audio/speech';
$headers = array(
'Content-Type: application/json',
'Authorization: Bearer ' . $apiKey
);
$data = array(
'model' => 'tts-1',
'input' => $text,
'voice' => 'alloy'
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode == 200) {
header('Content-Type: audio/mpeg');
header('Content-Disposition: attachment; filename="speech.mp3"');
echo $response;
} else {
http_response_code($httpCode);
echo 'Fehler beim Erstellen der Sprachausgabe: ' . $response;
}
}
?>