4 回答

TA貢獻1827條經驗 獲得超8個贊
如果第一個答案中提到的curl請求不起作用,您還可以使用PHP - Guzzle。
這是我的代碼:
if($langCode == 'fa'){
$region = 'uaenorth';
$key = 'key-for-the-resource-region';
$languageCode = 'fa-IR';
$voice = 'fa-IR-DilaraNeural';
}else{
$region = 'northeurope';
$key = 'key-for-the-resource-region';
$languageCode = 'fi-FI';
$voice = 'fi-FI-SelmaNeural';
}
$body = "<speak version='1.0' xml:lang='$languageCode'><voice xml:lang='$languageCode' name='$voice'>$content</voice></speak>";
$client = new Client();
$headers = [
'Content-Type' => ' application/ssml+xml',
'Ocp-Apim-Subscription-Key' => $key,
'X-Microsoft-OutputFormat' => 'audio-16khz-128kbitrate-mono-mp3'
];
$request = new Request('POST', "https://$region.tts.speech.microsoft.com/cognitiveservices/v1", $headers, $body);
$res = $client->sendAsync($request)->wait();
$response = $res->getBody();
// Check if the request was successful
if ($response !== false) {
return base64_encode($response);
} else {
return false;
}

TA貢獻1111條經驗 獲得超0個贊
如果您能確定一些事情,例如:
使用正確的區域,即如果您要轉換波斯語,則需要確保波斯語的 URL 中有正確的區域。
例如:https: //uaenorth.tts.speech.microsoft.com/cognitiveservices/v1
uaenorth特定于中東語言。
在您的 Azure 帳戶中,您需要為該區域創建資源。假設您在美國創建了一個資源,并且嘗試使用在美國創建的資源的密鑰轉錄波斯語,它將不起作用。
這是我的代碼,它并不完美,但它可以工作。這是我給你的想法。
if($langCode == 'fa'){
$region = 'uaenorth';
$key = 'Key-for-resource-created-in-uaenorth';
$languageCode = 'fa-IR';
$voice = 'fa-IR-DilaraNeural';
}else{
$region = 'northeurope';
$key = 'Key-for-resource-created-in-northeurope';
$languageCode = 'fi-FI';
$voice = 'fi-FI-SelmaNeural';
}
$data = "<speak version=\'1.0\' xml:lang=\'$languageCode\'>
<voice xml:lang=\'$languageCode\' name=\'$voice\'>
$content
</voice>
</speak>";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://$region.tts.speech.microsoft.com/cognitiveservices/v1",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>json_encode($data),
CURLOPT_HTTPHEADER => array(
'Content-Type: application/ssml+xml',
'Ocp-Apim-Subscription-Key: $key',
'X-Microsoft-OutputFormat: audio-16khz-128kbitrate-mono-mp3'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;

TA貢獻1804條經驗 獲得超8個贊
您可以將任何字符串放入 HTTP 請求的正文中。無論它是 URL 編碼、JSON、二進制、XML 還是其他形式。
對于像這樣的簡單情況 - 您可以創建一個簡單的字符串,其中包含請求所需的 XML。像這樣:
$params = "<speak version='1.0' xml:lang='en-US'><voice xml:lang='en-US' xml:gender='Female' name='en-US-AriaRUS'>Hello I need to be converted to Voice</voice></speak>";
但對于更復雜的 XML 并確保生成有效的 XML - 您可以使用 PHP XML 庫之一,例如:
https://www.php.net/manual/en/book.simplexml.php
https://www.php.net/manual/en/book.dom.php
您可以在其中以面向對象的方式構建 XML,然后將其轉換為有效的字符串,確保不存在任何語法錯誤。
CURL API 可能有點令人困惑,但請求的正文進入了CURLOPT_POSTFIELDS
. 這不僅適用于 POST url 編碼數據。
- 4 回答
- 0 關注
- 284 瀏覽
添加回答
舉報