Skip to content

Instantly share code, notes, and snippets.

@midnightcodr
Last active October 21, 2023 04:50
Show Gist options
  • Save midnightcodr/e597615332b7e5fa872bbb2fdb36e382 to your computer and use it in GitHub Desktop.
Save midnightcodr/e597615332b7e5fa872bbb2fdb36e382 to your computer and use it in GitHub Desktop.
aws translate with php (curl) without sdk
<?php
function aws_translate($text, $sourceLang = 'zh-TW', $targetLang = 'en')
{
$aws_access_key_id = 'key_id';
$aws_secret_access_key = 'key';
// AWS region and Host Name (Host names are different for each AWS region)
// As an example these are set to us-east-1 (US Standard)
$aws_region = 'us-east-1';
$host_name = 'translate.us-east-1.amazonaws.com';
$amz_target = 'AWSShineFrontendService_20170701.TranslateText';
// Server path where content is present
$version = '2017-07-01';
$operation = 'TranslateText';
$params = array(
'SourceLanguageCode' => $sourceLang,
'TargetLanguageCode' => $targetLang,
'Text' => $text
);
$content = json_encode($params);
// Service name for S3
$aws_service_name = 'translate';
// UTC timestamp and date
$timestamp = gmdate('Ymd\THis\Z');
$date = gmdate('Ymd');
// HTTP request headers as key & value
$request_headers = array();
$request_headers['content-type'] = 'application/x-amz-json-1.1';
$request_headers['x-amz-date'] = $timestamp;
$request_headers['x-amz-target'] = $amz_target;
$request_headers['host'] = $host_name;
// $request_headers['x-amz-content-sha256'] = hash('sha256', $content);
// Sort it in ascending order
ksort($request_headers);
// Canonical headers
$canonical_headers = [];
foreach ($request_headers as $key => $value) {
$canonical_headers[] = strtolower($key) . ":" . $value;
}
// The tailing new line must be kept, without it a signature mismatch error will be returned
$canonical_headers = implode("\n", $canonical_headers) . "\n";
// Signed headers
$signed_headers = 'content-type;host;x-amz-date;x-amz-target';
$canonical_uri = '/';
// Cannonical request
$canonical_request = [];
$canonical_request[] = "POST";
$canonical_request[] = $canonical_uri;
$canonical_request[] = "";
$canonical_request[] = $canonical_headers;
$canonical_request[] = $signed_headers;
$canonical_request[] = hash('sha256', $content);
$canonical_request = implode("\n", $canonical_request);
$hashed_canonical_request = hash('sha256', $canonical_request);
// AWS Scope
$scope = [];
$scope[] = $date;
$scope[] = $aws_region;
$scope[] = $aws_service_name;
$scope[] = "aws4_request";
// String to sign
$string_to_sign = [];
$string_to_sign[] = "AWS4-HMAC-SHA256";
$string_to_sign[] = $timestamp;
$string_to_sign[] = implode('/', $scope);
$string_to_sign[] = $hashed_canonical_request;
$string_to_sign = implode("\n", $string_to_sign);
// Signing key
$kSecret = 'AWS4' . $aws_secret_access_key;
$kDate = hash_hmac('sha256', $date, $kSecret, true);
$kRegion = hash_hmac('sha256', $aws_region, $kDate, true);
$kService = hash_hmac('sha256', $aws_service_name, $kRegion, true);
$kSigning = hash_hmac('sha256', 'aws4_request', $kService, true);
// Signature
$signature = hash_hmac('sha256', $string_to_sign, $kSigning);
// Authorization
$authorization = [
'Credential=' . $aws_access_key_id . '/' . implode('/', $scope),
'SignedHeaders=' . $signed_headers,
'Signature=' . $signature
];
$authorization = 'AWS4-HMAC-SHA256' . ' ' . implode(',', $authorization);
// Curl headers
$curl_headers = ['Authorization: ' . $authorization];
foreach ($request_headers as $key => $value) {
$curl_headers[] = $key . ": " . $value;
}
$url = "https://$host_name/";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $curl_headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
$response = curl_exec($ch);
if (curl_errno($ch)) {
return 'cURL error: ' . curl_error($ch);
}
// Close the cURL session
curl_close($ch);
// Extract the translated text from the response
$result = json_decode($response, true);
if (isset($result['TranslatedText'])) {
return $result['TranslatedText'];
} else {
// error_log("Translation not available for [$text]");
return $text;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment