php - 使用 PHP SDK 的 Amazon Polly 实现

标签 php amazon-web-services sdk amazon-polly

我正在尝试使用 Amazon 的 PHP SDK 将 Amazon Polly Web 服务集成到我的一个项目中。但是当我使用 PollyClient SDK 时,在客户端 createSynthesizeSpeechPreSignedUrl() 中只实现了一个方法,它返回一个 url,而不是音频剪辑。当我尝试将 url 粘贴到浏览器窗口时,出现以下错误:"message": "The security token included in the request is invalid."

请看我的代码片段:

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 错误是:

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:

有趣的是,我能够使用 Node.js SDK 生成音频剪辑​​,所以我非常确定访问 key 和 secret key 工作正常。

如果有人能指出如何将 PHP SDK 与示例代码或有用的链接一起使用,那就太好了。

最佳答案

下面是一些示例代码,用于在浏览器中将 TTS 下载为 .mp3 文件,关键部分是 $result->get('AudioStream')->getContents(),这是什么获取实际的 .mp3 数据。

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;

至于链接,这里有几个:

关于php - 使用 PHP SDK 的 Amazon Polly 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43320716/

相关文章:

php - php中如何获取数组中的唯一值?

php - 为什么 php 中的++ 运算符作用于对象属性?

MySQL 滚动总和

java - 在 DynamoDB 中执行更新时每个属性的多个条件表达式

javascript - 像 facebook 商业页面一样使用内置的像 open-graph api 不使用 javascript sdk

PHP 将 2 位数年份转换为 4 位数年份

php - 谷歌分析 API 关键字

node.js - 将 NodeJS 作为 Windows 服务托管

amazon-web-services - YAML_FILE_ERROR : YAML file does not exist

iOS Facebook SDK : Apprequests dialog doesn't show up