PHP Text to Speech using Amazon Polly API

Last modified on by Karthikeyan K

Amazon Polly is a Web Service used to convert any Text to Speech in real time. Also there is an option to download into MP3 format.

This text to speech service that uses advanced machine learning technologies to convert speech that sounds like a original human voice.

checkout my recent article on how to identify celebrities in an image using PHP and Amazon Rekognition API?

Why Amazon Polly?

It supports multiple languages across countries and variety of human voices. You can able to choose voices both male and female based on the country/region. It is awesome right? Yeah.

php text to speech using amazon polly API

Benefits of Amazon Polly – Text to Speech

  • Simple to Use
  • Low Cost – Pay as you go Pricing (On Demand)
  • Natural Human Voices
  • Speedy Response with Best user Experience
  • MP3 & OGG Support
  • Cloud Platform
  • FREE 5 Million Characters per Month (ie – You can read an average of 2000 emails/month)

Since there are so many APIs are available, I prefer Amazon polly because it is easy to add speech to your video, presentation, or online training course. Amazon Polly can generate speech in 24 languages, making it easy to add voice to applications with a global audience. With Amazon Polly you can read any content like RSS feed, news, or email, and store the speech in to MP3 audio formats – read more

Download and Install Amazon Polly PHP SDK Client

To get started, you must download the zip file, unzip it into your project to a location of your choosing, and include the autoloader:

require '/path/to/aws-autoloader.php';

Configuring the AWS Access Keys

In order to use amazon polly service, we should register an account with AWS and get the  AWS Access key/secret within our AWS account. Please go through this link to get the credentails

Creating Amazon Polly Client

After getting credentails from amazon account, we need to create an polly client as below

use Aws\Polly\PollyClient;

$config = [
'version' => 'latest',
'region' => 'us-east-1', //region
'credentials' => [
'key' => 'your aws access key',
'secret' => 'your aws secret key',
] ];

$client = new PollyClient($config);

Converting Text to Speech via Polly API

We just need to call the SynthesizeSpeech method, provide the text you wish to synthesize, select one of the available Text-to-Speech (TTS) voices, and specify an audio output format. Amazon Polly then synthesizes the provided text into a high-quality speech audio stream.

 VoiceId  – check this Polly Voice documentation

 OutputFormat – This will be mp3, ogg_vorbis, or pcm.

 TextType  – This will be plain text or SSML. The default value is plain text. For more information, see Using SSML.

$args = [
'OutputFormat' => 'mp3',
'Text' => "<speak><prosody rate='medium'>your text goes here..</prosody></speak>",
'TextType' => 'ssml',
'VoiceId' => "Joanna",

$result = $client->synthesizeSpeech($args);

$resultData = $result->get('AudioStream')->getContents();

Download MP3 or Listen the Text

Now you need to convert your text to speech by listening the text or by downloading the text into MP3 format

Listening the text

$size = strlen($resultData); // File size
$length = $size; // Content length
$start = 0; // Start byte
$end = $size - 1; // End byte
header("Content-Type: audio/mpeg");
header("Accept-Ranges: 0-$length");
header("Content-Range: bytes $start-$end/$size");
header("Content-Length: $length");
echo $resultData;

Download the Text to Speech in MP3 Format

header('Content-length: ' . strlen($resultData));
header('Content-Disposition: attachment; filename="polly-text-to-speech.mp3"');
header('X-Pad: avoid browser bug');
header('Cache-Control: no-cache');
echo $resultData;

take a quick demo now

I hope you like this article very much!. Please feel free to download the code and use it in your projects as well.

I will update this polly text to speech with multi language support using google translator in my next post.

You May Also Like

4 Responses

  1. Mohamad Hamouday says:

    I will try it.

  2. Hi Mohamed,

    Seems you are using windows server..

    To resolve the error, you need to define your CURL certificate authority information path

    To do that,

    Download the latest curl recognized certificates here:
    Save the cacert.pem file in a reachable destination.
    Then, in your php.ini file, scroll down to where you find [curl].
    You should see the CURLOPT_CAINFO option commented out. Uncomment and point it to the cacert.pem file. You should have a line like this:

    curl.cainfo = “certificate pathcacert.pem”

    Save and close your php.ini. Restart your webserver and try your request again.


  3. @disqus_0EUy39ZYLC:disqus is it working now ?

Leave a Reply

Your email address will not be published. Required fields are marked *