0

I am trying to implement code from this site and in the code the following line is causing an error and my page is not loaded --

use \Aws\Polly\PollyClient;

If I comment the code out then the page loads fine and I get no errors besides that the script I tried to use wont work.

Code seems pretty standard php so im wondering if it's cause it's wordpress and I would need to do something else to be to use - "use" ?

FULL CODE ---

//include_once ('/polly/aws-autoloader.php');

require_once ('/polly/aws-autoloader.php');
use \Aws\Polly\PollyClient;

if(isset($_REQUEST['voice']) && $_REQUEST['voice'] != '') {

$voice_id = $_REQUEST['voice'];

} else {

$voice_id="Joanna";

}

if(isset($_REQUEST['text']) && $_REQUEST['text'] != '') {

$text = trim(strip_tags($_REQUEST['text']));

}

if(isset($_REQUEST['rate']) && $_REQUEST['rate']!='') {

$rate=$_REQUEST['rate'];

} else {

$rate="medium";

}



$is_download = false;


if(isset($_REQUEST['download']) && $_REQUEST['download']==1) {

$is_download=true;

}


$config = [

'version'     => 'latest',
'region'      => 'us-east-1',
'credentials' => [
'key'    => 'MY KEYS',

'secret' => 'MY KEYS',
]

];

$client = new PollyClient($config);
$args = [

'OutputFormat' => 'mp3',
'Text' => "<speak><prosody rate='$rate'>".str_replace("&","&amp;",urldecode ($text))."</prosody></speak>",
'TextType' => 'ssml',
'VoiceId' => $voice_id,
];

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

$size = strlen($resultData); // File size

$length = $size; // Content length

$start = 0; // Start byte

$end = $size - 1; // End byte

if(!$is_download) {

header('Content-Transfer-Encoding:chunked');

header("Content-Type: audio/mpeg");

header("Accept-Ranges: 0-$length");

header("Content-Range: bytes $start-$end/$size");

header("Content-Length: $length");

echo $resultData;


} else {


header('Content-length: ' . strlen($resultData));

header('Content-Disposition: attachment; filename="polly-text-to-speech.mp3"');

header('X-Pad: avoid browser bug');

header('Cache-Control: no-cache');

echo $resultData;

}
5
  • Did you try debugging this? Could be a (very very) old PHP version on the server Commented May 7, 2018 at 14:27
  • @kero the php version is not old. Commented May 7, 2018 at 14:27
  • Please debug this a bit further. Without an error message it is just guessing. /polly/aws-autoloader.php seems off (see this thread) Commented May 7, 2018 at 14:29
  • I did and I get this -- Parse error: syntax error, unexpected 'use' (T_USE) in /var/...... Commented May 7, 2018 at 14:44
  • Which version of PHP are you using? Commented May 7, 2018 at 15:32

1 Answer 1

4

Quoting this answer, which came up when I searched for your error message: (emphasis mine)

You cannot use "use" where you are using it.

The "use" keyword is either in front of a class definition to import other classes/interfaces/traits into it's own namespace, or it is inside the class (but not inside a method) to add traits to the class.

The use has to be outside of the function I presume you are calling it in. Something like this should work

<?php

use \Aws\Polly\PollyClient;

// some code

function myfunc() {
    require_once ('/polly/aws-autoloader.php');
    // rest of the code
}

It should be no problem that you require the autoloader after the use keyword. After all, it only expands PollyClient to \Aws\Polly\PollyClient when you use it in the code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.