AWS Polly为PHP提供文本到语音的服务

Ank*_*wal 2 php amazon-web-services

如何在PHP SDK中使用AWS Polly服务.我正在使用下面的代码,但它没有生成文件:

$client = new \Aws\Polly\PollyClient(['version' => '2016-06-10','credentials' => $credentials,'region' => 'us-east-1']);            
$result = $client->synthesizeSpeech(['OutputFormat' => 'mp3','Text' => $text,'TextType' => 'text','VoiceId' => 'Emma']);
Run Code Online (Sandbox Code Playgroud)

man*_*gdi 5

$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' => $text,
'TextType' => 'text',
'VoiceId' => 'Emma',
]);

$resultData = $result->get('AudioStream')->getContents();

$local_mp3_file = $filePath . $FileName;
file_put_contents($local_mp3_file, $resultData); // write mp3 file content to file

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=$local_mp3_file');
header('X-Pad: avoid browser bug');
header('Cache-Control: no-cache');
Run Code Online (Sandbox Code Playgroud)