如何在 PHP 中正确实现 Microsoft 文本转语音

我正在尝试按照下面链接中的教程在 php 中实现 Microsoft 文本到语音。

微软API链接

这是 API 示例发布请求

POST /cognitiveservices/v1 HTTP/1.1


X-Microsoft-OutputFormat: raw-16khz-16bit-mono-pcm

Content-Type: application/ssml+xml

Host: westus.tts.speech.microsoft.com

Content-Length: 225

Authorization: Bearer [Base64 access_token]


<speak version='1.0' xml:lang='en-US'><voice xml:lang='en-US' xml:gender='Female'

    name='en-US-AriaRUS'>

        Microsoft Speech Service Text-to-Speech API

</voice></speak>

我已经获得了我的访问令牌。


这是我的问题。 如何添加我将转换为语音的文本。我试过这个


$params = "Hello I need to be converted to Voice"; 

但什么也没发生。没有错误消息。


这是到目前为止的完整编码


$url = 'https://westus.tts.speech.microsoft.com/cognitiveservices/v1';

$curl = curl_init();

$params = "Hello I need to be converted to Voice";



$myApp_name = 'Text-to-Speech-App';

$header = [

"Authorization: Bearer mytoken-goes-here",

    "Content-Type: application/ssml+xml",

"X-Microsoft-OutputFormat: raw-16khz-16bit-mono-pcm",

"User-Agent: $myApp_name",

"Content-Length:". strlen($params)

];



curl_setopt($curl, CURLOPT_URL, $url);

curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');

curl_setopt($curl, CURLOPT_HTTPHEADER, $header);

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

curl_setopt($curl, CURLOPT_POSTFIELDS, $params);


echo $response = curl_exec($curl);


摇曳的蔷薇
浏览 223回答 4
4回答

慕仙森

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

catspeake

如果您能确定一些事情,例如:使用正确的区域,即如果您要转换波斯语,则需要确保波斯语的 URL 中有正确的区域。例如:https:&nbsp;//uaenorth.tts.speech.microsoft.com/cognitiveservices/v1uaenorth特定于中东语言。在您的 Azure 帐户中,您需要为该区域创建资源。假设您在美国创建了一个资源,并且尝试使用在美国创建的资源的密钥转录波斯语,它将不起作用。这是我的代码,它并不完美,但它可以工作。这是我给你的想法。&nbsp; &nbsp; &nbsp; &nbsp;if($langCode == 'fa'){&nbsp; &nbsp; &nbsp; &nbsp; $region&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= 'uaenorth';&nbsp; &nbsp; &nbsp; &nbsp; $key&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = 'Key-for-resource-created-in-uaenorth';&nbsp; &nbsp; &nbsp; &nbsp; $languageCode&nbsp; &nbsp;= 'fa-IR';&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $voice&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = 'fa-IR-DilaraNeural';&nbsp;&nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; $region&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= 'northeurope';&nbsp; &nbsp; &nbsp; &nbsp; $key&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = 'Key-for-resource-created-in-northeurope';&nbsp; &nbsp; &nbsp; &nbsp; $languageCode&nbsp; &nbsp;= 'fi-FI';&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $voice&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = 'fi-FI-SelmaNeural';&nbsp; &nbsp; }&nbsp; &nbsp; $data = "<speak version=\'1.0\' xml:lang=\'$languageCode\'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <voice xml:lang=\'$languageCode\' name=\'$voice\'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $content&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </voice>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </speak>";&nbsp; &nbsp; $curl = curl_init();&nbsp; &nbsp; curl_setopt_array($curl, array(&nbsp; &nbsp; CURLOPT_URL => "https://$region.tts.speech.microsoft.com/cognitiveservices/v1",&nbsp; &nbsp; CURLOPT_RETURNTRANSFER => true,&nbsp; &nbsp; CURLOPT_ENCODING => '',&nbsp; &nbsp; CURLOPT_MAXREDIRS => 10,&nbsp; &nbsp; CURLOPT_TIMEOUT => 0,&nbsp; &nbsp; CURLOPT_FOLLOWLOCATION => true,&nbsp; &nbsp; CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,&nbsp; &nbsp; CURLOPT_CUSTOMREQUEST => 'POST',&nbsp; &nbsp; CURLOPT_POSTFIELDS =>json_encode($data),&nbsp; &nbsp; CURLOPT_HTTPHEADER => array(&nbsp; &nbsp; &nbsp; &nbsp; 'Content-Type: application/ssml+xml',&nbsp; &nbsp; &nbsp; &nbsp; 'Ocp-Apim-Subscription-Key: $key',&nbsp; &nbsp; &nbsp; &nbsp; 'X-Microsoft-OutputFormat: audio-16khz-128kbitrate-mono-mp3'&nbsp; &nbsp; ),&nbsp; &nbsp; ));&nbsp; &nbsp; $response = curl_exec($curl);&nbsp; &nbsp; curl_close($curl);&nbsp; &nbsp; echo $response;

慕尼黑5688855

如果其他人遇到此问题 - 首先您需要获取访问令牌,然后将其作为标头与原始代码一起发送。

胡说叔叔

您可以将任何字符串放入 HTTP 请求的正文中。无论它是 URL 编码、JSON、二进制、XML 还是其他形式。对于像这样的简单情况 - 您可以创建一个简单的字符串,其中包含请求所需的 XML。像这样:$params&nbsp;=&nbsp;"<speak&nbsp;version='1.0'&nbsp;xml:lang='en-US'><voice&nbsp;xml:lang='en-US'&nbsp;xml:gender='Female'&nbsp;name='en-US-AriaRUS'>Hello&nbsp;I&nbsp;need&nbsp;to&nbsp;be&nbsp;converted&nbsp;to&nbsp;Voice</voice></speak>";但对于更复杂的 XML 并确保生成有效的 XML - 您可以使用 PHP XML 库之一,例如:https://www.php.net/manual/en/book.simplexml.phphttps://www.php.net/manual/en/book.dom.php您可以在其中以面向对象的方式构建 XML,然后将其转换为有效的字符串,确保不存在任何语法错误。CURL API 可能有点令人困惑,但请求的正文进入了CURLOPT_POSTFIELDS.&nbsp;这不仅适用于 POST url 编码数据。
打开App,查看更多内容
随时随地看视频慕课网APP