2017-04-10 92 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

相關問題