Amazon Polly Implementation Using PHP SDK

I am trying to integrate Amazon Polly web service into one of my projects using the Amazon PHP SDK. But when I used the PollyClient SDK, there is createSynthesizeSpeechPreSignedUrl()

only one method implemented in the client and it returns a URL, not an audio clip. When I try to paste the URL in the browser window, I get the following error:"message": "The security token included in the request is invalid."

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;
    }
}

      

PHP error I am getting:

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:

      

Interesting part: I was able to create an audio clip using the Node.js SDK, so I'm pretty sure the passkey and secret are working fine.

It would be great if someone could point out how the PHP SDK can be used with sample code or helpful links.

+3


source to share


1 answer


Here is some sample code to load TTS as an .mp3 file in the browser, the key part $result->get('AudioStream')->getContents()

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;

      



A for links, here are a few:

+3


source







All Articles