I need a script to take my text, convert it to speech using AWS Polly API and save it on my server as mp3 file.
Currently when I load a page a player appears and plays back the speech clip but no file is downloaded.
What am I missing?
require '../../../include/lib/aws/aws-autoloader.php';
// Creating Amazon Polly Client
use Aws\Polly\PollyClient;
$config = [
'version' => 'latest',
'region' => 'us-west-2', //region
'credentials' => [
'key' => 'MY_KEY',
'secret' => 'my_AWS_secret',
]];
$client = new PollyClient($config);
// Converting Text to Speech via Polly API
$args = [
'OutputFormat' => 'mp3',
'Text' => "<speak><prosody rate='medium'>My text goes here..</prosody></speak>",
'TextType' => 'ssml',
'VoiceId' => "Joanna",
];
$result = $client->synthesizeSpeech($args);
$resultData = $result->get('AudioStream')->getContents();
// Listening the text
$size = strlen($resultData); // File size
$length = $size; // Content length
$start = 0; // Start byte
$end = $size - 1; // End byte
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;
// Download the Text to Speech in MP3 Format
header('Content-length: ' . strlen($resultData));
header('Content-Disposition: attachment; filename="./myfile.mp3"');
header('X-Pad: avoid browser bug');
header('Cache-Control: no-cache');
echo $resultData;
Your sending 2 responses one after the other. The first will be processed the second most likely will be ignored.
Basically you cant send headers after you already sent content. Refactor your code so you only send 1 response per request.
simply save the output in a new file:
file_put_contents("output.mp3", $resultData);
{ file_put_contents("output.mp3", $resultData);
This command worked for me as I was able to download the file on my local desktop
Related
I'm trying to upload a file with presigned AWS url.
I generate a presigned url like that :
use AsyncAws\S3\S3Client;
$s3 = new S3Client();
$bucket = 'my-bucket';
$key = 'myfile.pdf';
$date = new \DateTimeImmutable('+15minutes');
$contentType = 'application/pdf';
$input = new GetObjectRequest([
'Bucket' => $bucket,
'Key' => $key,
'ContentType' => $contentType
]);
$presignUrl = $s3->presign($input, $date);
This code above works fine, I get a presigned url like this : "https://my-bucket.s3.eu-west-3.amazonaws.com/myfile.pdf?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20220809T141153Z&X-Amz-Expires=900&X-Amz-Credential=XXXXXXXXXXXXXXX%2F20220809%2Feu-west-3%2Fs3%2Faws4_request&x-amz-content-sha256=UNSIGNED-PAYLOAD&X-Amz-SignedHeaders=host&X-Amz-Signature=XXXXXXXXXXXXXXXXXXXXXXXXXX"
Next, I try to use this url to upload a file with Postman.
I select my pdf file via Body / binary / Select file
This is my postman config and response :
As you can see, I have a 403 code response.
These are my headers (of course I've tried with differents Content-Type but nothing better than the exactly same 403 response.
Important information : With the same $s3 instance and authentification, if I try to get object url ($content = $s3->getObject($input); with AsyncAws), it works well. So I suppose, it is not an authentification issue.
Thanks in advance if you have any idea for me !
trying to generate a pre-signed query string to POST to an S3 bucket using the AWS SDK for PHP. Getting the following error:
The request signature we calculated does not match the signature you provided.
Here's the php file to generate the URL, adapted from here.
<?php
require('./aws/aws-autoloader.php');
$client = new \Aws\S3\S3Client([
'version' => 'latest',
'region' => 'us-east-1',
'credentials' => [
'key' => '[KEY]',
'secret' => '[SECRET]',
],
]);
$bucket = '[mybucket]';
// Set some defaults for form input fields
$formInputs = ['acl' => 'public-read'];
// Construct an array of conditions for policy
$options = [
['acl' => 'bucket-owner-full-control'],
['bucket' => $bucket],
];
// Optional: configure expiration time string
$expires = '+2 hours';
$postObject = new \Aws\S3\PostObjectV4(
$client,
$bucket,
$formInputs,
$options,
$expires
);
// Get attributes to set on an HTML form, e.g., action, method, enctype
$formAttributes = $postObject->getFormAttributes();
// Get form input fields. This will include anything set as a form input in
// the constructor, the provided JSON policy, your AWS Access Key ID, and an
// auth signature.
$formInputs = $postObject->getFormInputs();
echo "https://[mybucket].s3.amazonaws.com/?".http_build_query($formInputs)."&X-Amz-Expires=7200&X-Amz-SignedHeaders=host";
?>
And the curl command:
curl --request POST --upload-file "file.gif" -k [query string here]
I am uploading PDF's to AmazonS3 manually, using Panic Transmis and via a PHP script/API.
For some reason, some display in your browser, and some force download.
I have checked permission and can not seem to see any issues, Can anyone help explain how to make PDF's always display in browser ( unless the user specifies otherwise ).
I don't think it is as browser issue.
You need to change the Content-Type and Content-Disposition.
Content-Type: application/pdf;
Content-Disposition: inline;
Using the AWS S3 console, find the file and using the context menu (right click) select Properties then it's under Metadata.
Or change this programmatically:
http://docs.aws.amazon.com/AWSSDKforPHP/latest/index.html#m=AmazonS3/create_object
In companion with well's answer, here an example:
public function save($bucket, $name, $content, $options = [])
{
$this->s3->putObject([
'Bucket' => $bucket,
'Key' => $name,
'Body' => $content,
] + $options);
}
$this->bucket->save('my-bucket', 'SofiaLoren.pdf', $content, [
'ContentType' => 'application/pdf',
'ContentDisposition' => 'inline',
]);
I am developing a web application. Here I have implemented the authorize.net payment gateway.
But here the cave code is not validating correctly. If I give wrong card code the payment results in success But i need the result as payment declained
Does anyone know this?
see the code
"x_version" => "3.1",
"x_delim_data" => "TRUE",
"x_delim_char" => "|",
"x_relay_response" => "FALSE",
"x_type" => "AUTH_CAPTURE",
"x_method" => "CC",
"x_card_num" => $card_number,
"x_exp_date" => $exp_date,
"x_amount" => $amount,
"x_description" => "Live Transaction",
"x_card_code" => $ccv,
"x_first_name" => $bill_name,
"x_address" => $bill_address,
"x_city" => $bill_city,
"x_state" => $bill_state,
"x_zip" => $bill_zip,
"x_country" => $bill_country,
"x_phone" => $bill_phone,
"x_email" => $email,
"x_ship_to_first_name" => $ship_name,
"x_ship_to_address" => $ship_address,
"x_ship_to_city" => $ship_city,
"x_ship_to_state" => $ship_state,
"x_ship_to_zip" => $ship_zip,
"x_ship_to_country" => $ship_country,
"x_ship_to_phone" => $ship_phone
// Additional fields can be added here as outlined in the AIM integration
// guide at: http://developer.authorize.net
);
// This section takes the input fields and converts them to the proper format
// for an http post. For example: "x_login=username&x_tran_key=a1B2c3D4"
$post_string = "";
foreach( $post_values as $key => $value )
{ $post_string .= "$key=" . urlencode( $value ) . "&"; }
$post_string = rtrim( $post_string, "& " );
// The following section provides an example of how to add line item details to
// the post string. Because line items may consist of multiple values with the
// same key/name, they cannot be simply added into the above array.
//
// This section is commented out by default.
foreach( $line_items as $value )
{ $post_string .= "&x_line_item=" . urlencode( $value ); }
// This sample code uses the CURL library for php to establish a connection,
// submit the post, and record the response.
// If you receive an error, you may want to ensure that you have the curl
// library enabled in your php configuration
$request = curl_init($post_url); // initiate curl object
curl_setopt($request, CURLOPT_HEADER, 0); // set to 0 to eliminate header info from response
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1); // Returns response data instead of TRUE(1)
curl_setopt($request, CURLOPT_POSTFIELDS, $post_string); // use HTTP POST to send form data
curl_setopt($request, CURLOPT_SSL_VERIFYPEER, FALSE); // uncomment this line if you get no gateway response.
$post_response = curl_exec($request); // execute curl post and store results in $post_response
// additional options may be required depending upon your server configuration
// you can find documentation on curl options at http://www.php.net/curl_setopt
curl_close ($request); // close curl object
// This line takes the response and breaks it into an array using the specified delimiting character
$response_array = explode($post_values["x_delim_char"],$post_response);
You problem might be related to some incorrect settings in the authorize.net account. Check the following:
Make sure that the transaction version is set to 3.1. You can do that from Settings->Transaction Version->Submit
Set the Card Code Verification rejection settings and select N and U options. To do that, go to Settings->Card Code Verification
Make sure that the x_version field is set to "3.1" and that the x_card_code contains CCV number.
Hope this helps.
More details:
https://support.authorize.net/authkb/index?page=content&id=A546&impressions=false
It's not a code error. If you want the payment to be declined when the CVV number is incorrect you need to set it in your Authorize.Net control panel. It is found under the security settings.
How do i upload a photo with tagging info using graph api ?
the php solution i found is at http://forum.developers.facebook.net/viewtopic.php?id=86152
$data = array(
'tag_uid' => $friend,
'x' => rand() % 100,
'y' => rand() % 100
);
$datatags[] = $data;
$attachment = array(
'access_token' => $access_token,
'tags' => $datatags
);
$facebook->setFileUploadSupport(true);
$attachment['image'] ='#'.realpath($filename);
$photo = $facebook->api('/'.$aid.'/photos', 'POST', $attachment);
just need to know the syntax to add multiple tags.
thanks.
You can set multiple tags by passing array of tag arrays like in this example: Uploading image and tagging multiple people in one step using PHP Facebook Graph API