Skip to content

Instantly share code, notes, and snippets.

@midnightcodr
Last active October 21, 2023 04:50

Revisions

  1. midnightcodr revised this gist Oct 21, 2023. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions translate.php
    Original file line number Diff line number Diff line change
    @@ -20,8 +20,6 @@ function aws_translate($text, $sourceLang = 'zh-TW', $targetLang = 'en')
    );
    $content = json_encode($params);

    // AWS file permissions
    $content_acl = 'authenticated-read';


    // Service name for S3
  2. midnightcodr created this gist Oct 20, 2023.
    131 changes: 131 additions & 0 deletions translate.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,131 @@
    <?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);

    // AWS file permissions
    $content_acl = 'authenticated-read';


    // 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;
    }
    }