-1

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;

}

closed as off-topic by Mark Kaplun, Krzysiek Dróżdż, Jacob Peattie, Jack Johansson May 9 '18 at 23:35

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • Did you try debugging this? Could be a (very very) old PHP version on the server – kero May 7 '18 at 14:27
  • @kero the php version is not old. – Rich May 7 '18 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) – kero May 7 '18 at 14:29
  • I did and I get this -- Parse error: syntax error, unexpected 'use' (T_USE) in /var/...... – Rich May 7 '18 at 14:44
  • Which version of PHP are you using? – Tom J Nowell May 7 '18 at 15:32
1

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.