PHP code example of trisnawan / translator-client-php

1. Go to this page and download the library: Download trisnawan/translator-client-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

trisnawan / translator-client-php example snippets


use Trisnawan\Translator\Translator;
use Trisnawan\Translator\Exception\ApiException;

$translator = new Translator(
    baseUrl: 'http://localhost:3000',
    accountId: '01a0eea2-221c-70cd-83d0-56e95a65c37b', // UUID akun pemilik key
    keyId: '01a0eea2-2296-71ae-bd0b-9bee0cd7106f',     // id pada account_keys
    secretKey: 'sk_translator_...',                    // hanya diketahui Anda
    from: 'id',                                        // bahasa default
    to: 'en',
);

$result = $translator->translate(
    driver: 'gemini-3.8-flash',
    referenceId: 'INV-2026-0001',
    referenceContent: 'Selamat pagi, apa kabar?',
);

// $result['history_id'], $result['status'], $result['callback_enabled'], ...

$translator->translate('gemini-3.8-flash', 'INV-2026-0002', 'Hello', from: 'en', to: 'id');

use Trisnawan\Translator\Webhook;
use Trisnawan\Translator\Exception\WebhookException;

try {
    $data = (new Webhook())->validData($keyId, $secretKey);
} catch (WebhookException $e) {
    http_response_code(401);
    exit($e->getMessage());
}

if ($data['status'] === 'translated') {
    saveTranslation($data['reference_id'], $data['translated_content']);
}

http_response_code(200); // balas 2xx agar server tidak mengirim ulang

// Laravel / Symfony
$webhook = new Webhook($request->server->all(), $request->getContent());

// Pengujian
$webhook = new Webhook(['HTTP_KEY_ID' => $keyId, 'HTTP_AUTHORIZATION' => 'Bearer ' . $token], $jsonBody);

try {
    $result = $translator->translate('gemini-3.8-flash', 'INV-1', 'Halo');
} catch (ApiException $e) {
    // $e->getStatusCode() === 429, $e->getErrors() berisi detail quota
} catch (TranslatorException $e) {
    // API tidak dapat dihubungi, payload bukan UTF-8, dsb.
}
bash
composer