Search code examples
azurecurltext-to-speech

Azure: Get TTS File with Curl -Cognitive Speech


How do I get a correct TTS Response back, with an audio file? The token works, but the TTS request not!

{"code":"404","message": "Resource not found"}

For the Token iam using this URL: https://westeurope.api.cognitive.microsoft.com/sts/v1.0/issuetoken

i found this Code Example on Azure this site, but its not working :-/ (https://learn.microsoft.com/en-us/azure/cognitive-services/speech-service/rest-text-to-speech#sample-request-1)

$token=getToken();
$cont="<speak version='1.0' xml:lang='en-US'><voice xml:lang='en-US' xml:gender='Male'
    name='en-US-ChristopherNeural'>
        Microsoft Speech Service Text-to-Speech API
</voice></speak>";

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://westeurope.api.cognitive.microsoft.com/cognitiveservices/v1');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, ($cont));

$headers = array();
#$headers[] = 'Ocp-Apim-Subscription-Key: xxxxxxxxxxxx';

$headers[] = 'X-Microsoft-OutputFormat: raw-24khz-16bit-mono-pcm';
$headers[] = 'Content-Type: application/ssml+xml';
#$headers[] = 'Host: westus.tts.speech.microsoft.com';
$headers[] = 'Content-Length: '.strlen($cont);
$headers[] = 'Authorization: Bearer '.$token;//Token okay

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

 $result = curl_exec($ch);
 print_r($result); //{"code":"404","message": "Resource not found"} 

Solution

  • OK, now it works,

    i put the option curl_setopt($ch, CURLOPT_HEADER, 1); to get some more information about the response, and it shows me this error

    Required header User-Agent missing.

    so i add one more headerParameter to it and now it works:

    $headers[] = 'User-Agent: myApp/1.0.0';