<?php
header('Content-Type: application/json');
$allowed_voices = ["alloy", "echo", "fable", "onyx", "nova",
"shimmer", "coral", "verse", "ballad", "ash",
"sage", "amuch", "dan"];
$default_voice = 'alloy';
$audio_dir = 'tts_audio';
if (!file_exists($audio_dir)) {
if (!mkdir($audio_dir, 0755, true)) {
echo json_encode(['error' => 'تعذر إنشاء مجلد التخزين']);
exit;
}
}
$text = $_GET['txt'] ?? '';
$voice = $_GET['voice'] ?? $default_voice;
if (empty($text)) {
echo json_encode(['error' => 'النص مطلوب (استخدم txt=)']);
exit;
}
if (!in_array($voice, $allowed_voices)) {
$voice = $default_voice;
}
$filename = 'tts_'.md5($text.$voice.time()).'.mp3';
$local_path = $audio_dir.'/'.$filename;
if (file_exists($local_path)) {
echo json_encode([
'success' => true,
'url' => get_full_url($local_path),
'cached' => true
]);
exit;
}
$session_hash = substr(uniqid(), 0, 12);
$headers = [
'authority: nihalgazi-text-to-speech-unlimited.hf.space',
'accept: */*',
'content-type: application/json',
];
$payload = [
"data" => [$text, $voice, "", true, 12345],
"event_data" => null,
"fn_index" => 1,
"trigger_id" => 13,
"session_hash" => $session_hash
];
$ch = curl_init('https://nihalgazi-text-to-speech-unlimited.hf.space/gradio_api/queue/join');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($payload),
CURLOPT_TIMEOUT => 30
]);
$response = curl_exec($ch);
curl_close($ch);
$data_url = "https://nihalgazi-text-to-speech-unlimited.hf.space/gradio_api/queue/data?session_hash=$session_hash";
$ch = curl_init($data_url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array_filter($headers, fn($h) => !str_starts_with($h, 'content-type:')),
CURLOPT_TIMEOUT => 30
]);
$audio_response = curl_exec($ch);
curl_close($ch);
$audio_url = null;
foreach (explode("\n", $audio_response) as $line) {
if (str_starts_with($line, 'data: ')) {
$data = json_decode(substr($line, 6), true);
if (isset($data['output']['data'][0]['url'])) {
$audio_url = $data['output']['data'][0]['url'];
break;
}
}
}
if (!$audio_url) {
echo json_encode(['error' => 'فشل في توليد الصوت']);
exit;
}
$audio_content = @file_get_contents($audio_url);
if ($audio_content === false) {
echo json_encode(['error' => 'فشل في تحميل الملف الصوتي', 'audio_url' => $audio_url]);
exit;
}
if (file_put_contents($local_path, $audio_content) === false) {
echo json_encode(['error' => 'فشل في حفظ الملف على الخادم']);
exit;
}
echo json_encode([
'success' => true,
'url' => get_full_url($local_path),
'voice' => $voice,
'file_size' => filesize($local_path).' bytes'
]);
function get_full_url($path) {
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http";
$host = $_SERVER['HTTP_HOST'];
$script_path = dirname($_SERVER['SCRIPT_NAME']);
return "$protocol://$host$script_path/$path";
}
?>
API اصوات من OPEN AI 📱
بعد ما ترفعوا مثال 📱
DOMINVOICE.PHP?txt=HI [laugh]&style=laugh&voice=nova
المطور 🦄
@G_T_R_F