2017-04-10 87 views
3

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

请看看我的代码片段:

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生成音频剪辑​​,所以我很确定访问密钥和密钥工作正常。

如果任何人都可以指出PHP SDK如何与示例代码或有用的链接一起使用,那将是一件好事。

回答

3

下面是一些示例代码,在浏览器中下载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; 

的链接,这里有几个:

+0

如何在使用TextType作为SSML时增加字符长度?因为它只允许我每个PHP的MP3文件转换大约1450-1500个字符... @David –

+0

我不知道怎么做@JigneshVagh,但你可以看看这个[SO帖子](https:// stackoverflow.com/q/41317999/3088508)的一些想法。每个API调用只允许1500个_billed_字符。 [docs](http://docs.aws.amazon.com/polly/latest/dg/limits.html) –

+1

我前段时间实施了一项ivona服务,现已被亚马逊收购。用你的例子,我立即将我的代码转换为polly。救命稻草! – m47730

相关问题