How to properly implement microsoft text to speech in PHP

Am trying to implement microsoft text to speech in php by following the tutorials in the link below.

Microsoft API Link

here is the API Sample post request

POST /cognitiveservices/v1 HTTP/1.1

X-Microsoft-OutputFormat: raw-16khz-16bit-mono-pcm
Content-Type: application/ssml+xml
Content-Length: 225
Authorization: Bearer [Base64 access_token]

<speak version='1.0' xml:lang='en-US'><voice xml:lang='en-US' xml:gender='Female'
        Microsoft Speech Service Text-to-Speech API

I have already obtained my access token.

Here is my Issue. How to I added the text that I will be converted to speech. I have tried tried this

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

but nothing error message.

Here is the entire coding so far

$url = '';
$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);


  • You can put any string in the body of an HTTP request. No matter if it's URL encoded, JSON, Binary, XML or something else.

    For simple cases like this one - you can create a simple string, containing the XML that you need for your request. Like this:

    $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>";

    But for more complicated XMLs and to be sure that you're generating valid XML - you can use one of PHP XML libraries like:

    Where you can build the XML in a object-oriented way and then convert it to a valid string, making sure there isn't any syntax error.

    And the CURL API may be a bit confusing, but the Body of the request goes into CURLOPT_POSTFIELDS. This is not only for POST url-encoded data.