PHP code example of ahmadmayahi / php-amazon-polly

1. Go to this page and download the library: Download ahmadmayahi/php-amazon-polly library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ahmadmayahi / php-amazon-polly example snippets


use AhmadMayahi\Polly\Config;

$config = (new Config())
    ->setKey('AWS_KEY')
    ->setSecret('AWS_SECRET')
    ->setRegion('eu-west-1'); // default is: us-east-1

use AhmadMayahi\Polly\Voices\English\UnitedStates;
use AhmadMayahi\Polly\Polly;

$polly = Polly::init($config);

$speechFile = $polly
    // Desired voice
    ->voiceId(UnitedStates::Joanna)
    
    // Default is MP3.
    // Available options: asMp3(), asOgg(), asPcm(), asJson()
    ->asOgg()
    
    // Desired Text
    ->text('Hello World')
 
    // Convert and return the object
    ->convert();

convert('/path/to/desire/file/voice.mp3');

voiceId('Joanna')

use AhmadMayahi\Polly\Enums\OutputFormat;
use AhmadMayahi\Polly\Polly;

$polly = Polly::init($config);

$speechFile = $polly
    ->voiceId(UnitedStates::Joanna)
   
    // As enum
    ->outputFormat(OutputFormat::Ogg)
    
    // Or as a string
    ->outputFormat('ogg')
    
    ->text('Hello World')
 
    ->convert();

use AhmadMayahi\Polly\Enums\SpeechMarkType;
use AhmadMayahi\Polly\Voices\English\UnitedStates;
use AhmadMayahi\Polly\Polly;

$polly = Polly::init($config);

$speechFile = $polly
    ->voiceId(UnitedStates::Joanna)
    ->text('Hello World')
    ->withSpeechMarks(SpeechMarkType::Word, SpeechMarkType::Sentence)
    ->convert();

Array
(
    [0] => AhmadMayahi\Polly\Data\SpeechMark Object
        (
            [time] => 0
            [type] => AhmadMayahi\Polly\Enums\SpeechMarkType Enum:string
                (
                    [name] => Sentence
                    [value] => sentence
                )

            [start] => 7
            [end] => 18
            [value] => Hello World
        )

    [1] => AhmadMayahi\Polly\Data\SpeechMark Object
        (
            [time] => 6
            [type] => AhmadMayahi\Polly\Enums\SpeechMarkType Enum:string
                (
                    [name] => Word
                    [value] => word
                )

            [start] => 7
            [end] => 12
            [value] => Hello
        )

    [2] => AhmadMayahi\Polly\Data\SpeechMark Object
        (
            [time] => 273
            [type] => AhmadMayahi\Polly\Enums\SpeechMarkType Enum:string
                (
                    [name] => Word
                    [value] => word
                )

            [start] => 13
            [end] => 18
            [value] => World
        )
)

use AhmadMayahi\Polly\Voices\English\UnitedStates;
use AhmadMayahi\Polly\Polly;

$text = <<<EOL
<speak>
     He was caught up in the game.<break time="1s"/> In the middle of the 
     10/3/2014 <sub alias="World Wide Web Consortium">W3C</sub> meeting, 
     he shouted, "Nice job!" quite loudly. When his boss stared at him, he repeated 
     <amazon:effect name="whispered">"Nice job,"</amazon:effect> in a 
     whisper.
</speak>
EOL;

$polly = Polly::init($config);

$speechFile = $polly
    ->voiceId(UnitedStates::Ivy)
    ->text($text)
    ->convert();

use AhmadMayahi\Polly\Voices\English\UnitedStates;
use AhmadMayahi\Polly\Polly;

$polly = Polly::init($config);

$speechFile = $polly
    ->voiceId(UnitedStates::Kendra)
    ->text('Hello World')
    ->neuralVoice()
    ->convert();

use AhmadMayahi\Polly\Polly;

$polly = Polly::init($config);

$speechFile = $speech
    ->englishUnitedStatesJoanna($neural = true)
    ->text('Hello World')
    ->convert();

use AhmadMayahi\Polly\Voices\English\Australian;

Australian::Nicole;

use AhmadMayahi\Polly\Voices\English\Australian;

Australian::Nicole->describe();
bash
composer