Jayesh Ambali
Jayesh Ambali

Reputation: 221

Amazon Polly Implementation Using PHP SDK

I am trying to integrate the Amazon Polly web service into one of my projects using Amazon's PHP SDK. But when I used the PollyClient SDK, there is only one method implemented in the client createSynthesizeSpeechPreSignedUrl() and it returns a url, not the audio clip. When I try and paste the url in the browser window I get the following error: "message": "The security token included in the request is invalid."

Please see my code snippet:

error_reporting(E_ALL);
ini_set('display_errors', 1);
header('Content-Type: text/plain; charset=utf-8');
require_once 'app/aws/aws-autoloader.php';
use Aws\Polly\PollyClient;

class TestPolly extends Base {
    public function newPolly () {
        $connection = [
            'region'      => 'us-west-2',
            'version'     => 'latest',
            'debug'       => true,
            'scheme'      => 'http',
            'credentials' => [
                'key'    => 'XXXXX',
                'secret' => 'XXXXXX',
            ],
        ];
        $client     = new PollyClient($connection);
        $polly_args = [
            'OutputFormat' => 'mp3',
            'Text'         => 'My Input text',
            'TextType'     => 'text',
            'VoiceId'      => 'Brain',
        ];
        $result     = $client->synthesizeSpeech($polly_args);

        echo '<pre>';
        print_r($result);
        exit;
    }
}

The PHP error I am getting is:

Fatal error Uncaught exception 'Aws\Polly\Exception\PollyException' with message 'Error executing &quot;SynthesizeSpeech on http://polly.us-west-2.amazonaws.com/v1/speech

 AWS HTTP error: cURL error 7: Failed to connect to polly.us-west-2.amazonaws.com port 80: Connection refused (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)'

exception 'GuzzleHttp\Exception\ConnectException' with message 'cURL error 7: Failed to connect to polly.us-west-2.amazonaws.com port 80: Connection refused (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)' in D:\xampp\htdocs\sim_aws\aws\GuzzleHttp\Handler\CurlFactory.php:186
Stack trace:

The interesting part is, I was able to generate the audio clip using Node.js SDK, so I'm quite sure that the access key and secret key is working fine.

It would be great if anyone can point out how the PHP SDK can be used with sample code or useful links.

Upvotes: 7

Views: 6417

Answers (1)

Ethan
Ethan

Reputation: 4375

Here is some sample code to download the TTS as an .mp3 file in the browser, the crucial part is $result->get('AudioStream')->getContents(), this is what gets the actual .mp3 data.

require_once 'app/aws/aws-autoloader.php';
$awsAccessKeyId = 'XXXXXXX';
$awsSecretKey   = 'XXXXXXX';
$credentials    = new \Aws\Credentials\Credentials($awsAccessKeyId, $awsSecretKey);
$client         = new \Aws\Polly\PollyClient([
    'version'     => '2016-06-10',
    'credentials' => $credentials,
    'region'      => 'us-east-1',
]);
$result         = $client->synthesizeSpeech([
    'OutputFormat' => 'mp3',
    'Text'         => "My input text",
    'TextType'     => 'text',
    'VoiceId'      => 'Joanna',
]);
$resultData     = $result->get('AudioStream')->getContents();

header('Content-Transfer-Encoding: binary');
header('Content-Type: audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3');
header('Content-length: ' . strlen($resultData));
header('Content-Disposition: attachment; filename="pollyTTS.mp3"');
header('X-Pad: avoid browser bug');
header('Cache-Control: no-cache');

echo $resultData;

As for links, here are a few:

Upvotes: 11

Related Questions