Google trends Public Holidays Coupon Code Code Compiler

Text to Speech using Amazon Polly API in PHP

Jun 26, 2021

Text to Speech using Amazon Polly API in PHP

Amazon Polly is a cloud service that allows us to convert any Text to Speech in real-time. Also, there is an option to download into MP3 format.
Converting Text to Speech using Amazon Polly API in PHP

Text to speech (TTS) technology allows you to convert written text into spoken words, opening up various possibilities for enhancing user experiences, accessibility, and interaction with applications. Amazon Polly is a cloud service that provides a lifelike TTS solution, and in this guide, we'll explore how to integrate it into your PHP applications to create dynamic and engaging voice interactions.

Getting Started

Before you can start using Amazon Polly in your PHP application, there are a few initial steps you need to take:

Step 1: Install the AWS SDK for PHP

Amazon Polly is part of the AWS ecosystem. To interact with Polly, you'll need the AWS SDK for PHP. You can conveniently install it using Composer or Download AWS SDK to convert text to speech.:

This cloud service that uses a new machine learning technologies to convert text to speech. They offer customers the most natural and human like a original human voice.

You may read more about this service on AWS documentation.

If you don't want to build a PHP application to convert text to speech, then you can use the amazon Polly console directly. You can access it by login into your AWS account.

Step 2: Create an AWS Polly Client

To begin using Amazon Polly in your PHP script, you must create an AWS Polly client. This involves configuring AWS credentials, specifying your desired region, and setting up the Polly client. Here's a basic example:

To create the PHP application for Amazon Polly you need to get your AWS security credentials first. You can obtain it by login into the AWS account and then click on 'My Security Credentials' under the my account tab.

require '/amazon-polly-lib/aws-autoloader.php';

use Aws\Polly\PollyClient;

if(isset($_REQUEST['voice']) && $_REQUEST['voice'] != '')
    $voice = $_REQUEST['voice'];
} else {

if(isset($_REQUEST['text']) && $_REQUEST['text'] != '')
      $text = trim(strip_tags($textData));


   'version'     => 'latest',
   'region'      => 'us-east-1',
   'credentials' => [
    'key'    => 'YOUR_AWS_KEY',
    'secret' => 'YOUR_AWS_SECRET',

$client = new PollyClient($config);
$args = [
   'OutputFormat' => 'mp3',
   'Text' =>  "".str_replace("&","&",urldecode ($text))."",
   'TextType'     => 'ssml',
   'VoiceId' => $voice,
$result  = $client->synthesizeSpeech($args);          

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

$size   = strlen($resultData); // File size
$length = $size;           // Content length
$start  = 0;               // Start byte
$end    = $size - 1;       // End byte

if(!$is_download) {
header("Content-Type: audio/mpeg");
header("Accept-Ranges: 0-$length");
header("Content-Range: bytes $start-$end/$size");
header("Content-Length: $length");
echo $resultData;
} else {
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;

In this code, you replace 'YOUR_AWS_KEY' and 'YOUR_AWS_SECRET' with your AWS access key and secret access key. The 'us-east-1' region is just an example; you can choose the AWS region that best suits your needs.

Synthesizing Speech with Amazon Polly

Once your AWS Polly client is set up, you can start converting text into speech. In the example, we take a simple text string and synthesize it into speech. You can choose the output format, such as 'mp3', and even select different voices. The synthesized speech is then saved as an audio file ('output.mp3' in this case).


In conclusion, Amazon Polly API offers a powerful and straightforward way to introduce text-to-speech capabilities into your PHP applications. This technology has a wide range of applications, including assistive technologies, interactive voice response systems, and more. With just a few lines of PHP code, you can provide a more engaging and accessible experience for your users.

Text-to-speech technology has the potential to transform how users interact with your applications, making it an essential tool in the realm of modern web development. As you continue to explore and implement Amazon Polly in your projects, you'll unlock new possibilities for dynamic and personalized user experiences.

Copyright 2024. All rights are reserved