### Client usage examples Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/api-reference/stylerules.md Examples of using the client to create, update, and delete custom instructions, and handling errors. ```APIDOC ## Client Usage Examples ### Description Examples of creating, updating, and deleting custom instructions via the DeepL client. ### Create Custom Instruction ```php $instruction = $client->createStyleRuleCustomInstruction($rule, 'Formality', 'Always use formal language', 'de'); echo "Instruction ID: {$instruction->id}"; ``` ### Update Custom Instruction ```php $updated = $client->updateStyleRuleCustomInstruction($rule, $instruction->id, 'Updated Label', 'Use very formal language'); ``` ### Delete Custom Instruction ```php $client->deleteStyleRuleCustomInstruction($rule, $instruction->id); ``` ### Error Handling Server-side rejections surface as `DeepLException` (or `NotFoundException` when the style rule was deleted meanwhile): ```php try { $client->getStyleRule('nonexistent-id'); } catch (\DeepL\NotFoundException $e) { // style rule not found } ``` ``` -------------------------------- ### Language class constructor and example Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/api-reference/results.md Language constructor and an example of iterating target languages to check formality support. ```php public function __construct(string $name, string $code, ?bool $supportsFormality) ``` ```php foreach ($client->getTargetLanguages() as $lang) { if ($lang->supportsFormality) { echo $lang->name . ' (' . $lang->code . ') supports formality'; } } ``` -------------------------------- ### Create a glossary (monolingual and multilingual) Source: https://github.com/deepl/deepl-php/blob/main/upgrading_to_multilingual_glossaries.md Both examples require $deeplClient to be an initialized DeepL client. The monolingual example creates a glossary with a single source-target language pair. The multilingual example creates a glossary with one or more dictionary entries, each specifying its own source and target language. ```php // monolingual glossary example $entries = GlossaryEntries::fromEntries(['artist' => 'Maler', 'prize' => 'Gewinn']); $myGlossary = $deeplClient->createGlossary('My glossary', 'en', 'de', $entries); ``` ```php // multilingual glossary example $entries = GlossaryEntries::fromEntries(['artist' => 'Maler', 'prize' => 'Gewinn']); $myGlossaryDict = new MultilingualGlossaryDictionaryEntries('en', 'de', $entries); $myGlossary = $deeplClient->createMultilingualGlossary('My glossary', [$myGlossaryDict]); ``` -------------------------------- ### Usage class methods and example Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/api-reference/results.md Usage class constructor, anyLimitReached method, and an example of using getUsage() to check character usage and limit reached. ```php public function __construct(string $content) // @throws InvalidContentException ``` ```php public function anyLimitReached(): bool // true if any non-null subtype's limitReached() is true ``` ```php $usage = $client->getUsage(); if ($usage->character) { echo 'Characters: ' . $usage->character->count . ' of ' . $usage->character->limit; } if ($usage->anyLimitReached()) { echo 'Translation limit exceeded.'; } ``` -------------------------------- ### Example: Create and Download Translation Memory Export Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/api-reference/translation-memories.md Example code showing how to create an export, wait for it to complete, and download the resulting TMX file. ```APIDOC ## Example: Create and Download Translation Memory Export ### Description Demonstrates using the client methods `createTranslationMemoryExport`, `waitUntilTranslationMemoryJobDone`, and `downloadTranslationMemoryExport` to export a translation memory. ### Method N/A (SDK usage example) ### Endpoint N/A (SDK usage example) ### Parameters - `$tmId` - The translation memory ID for the export (not shown in the snippet but implied) - `$client` - An instance of the DeepL client ### Request Example ```php $created = $client->createTranslationMemoryExport($tmId); echo $created->reusedExisting ? "Reused an existing export\n" : "Started a new export\n"; $job = $client->waitUntilTranslationMemoryJobDone($created->jobId); $client->downloadTranslationMemoryExport($job, '/path/to/exported.tmx'); ``` ### Response No direct response shown; the example outputs a message and downloads the file. ``` -------------------------------- ### getGlossaryEntries usage example Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/api-reference/translator.md Example of fetching glossary entries and dumping the associative array of source-target pairs. ```php $entries = $translator->getGlossaryEntries($glossaryId); var_dump($entries->getEntries()); // array(source => target, ...) ``` -------------------------------- ### downloadDocument error handling example Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/api-reference/translator.md Example of catching DocumentNotReadyException when the document is not ready for download yet (HTTP 503). ```php try { $translator->downloadDocument($handle, '/tmp/out.docx'); } catch (\DeepL\DocumentNotReadyException $e) { // HTTP 503 — document is not ready for download yet } ``` -------------------------------- ### Get a glossary (monolingual and multilingual) Source: https://github.com/deepl/deepl-php/blob/main/upgrading_to_multilingual_glossaries.md Both examples fetch a glossary by its identifier. The monolingual variant returns a GlossaryInfo object, while the multilingual variant returns a MultilingualGlossaryInfo object. ```php // monolingual glossary example $glossaryId = '559192ed-8e23-...'; $myGlossary = $deeplClient->getGlossary($glossaryId); // GlossaryInfo object ``` ```php // multilingual glossary example $glossaryId = '559192ed-8e23-...'; $myGlossary = $deeplClient->getMultilingualGlossary($glossaryId); // MultilingualGlossaryInfo object ``` -------------------------------- ### createGlossary usage example Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/api-reference/translator.md Example of creating a glossary with entries from an associative array. The glossary is created with name 'My glossary', from English to German. ```php $entries = \DeepL\GlossaryEntries::fromEntries(['artist' => 'Maler', 'prize' => 'Gewinn']); $glossary = $translator->createGlossary('My glossary', 'en', 'de', $entries); ``` -------------------------------- ### List and delete glossaries (monolingual and multilingual) Source: https://github.com/deepl/deepl-php/blob/main/upgrading_to_multilingual_glossaries.md Both examples iterate over all glossaries and delete any whose name matches 'Old glossary'. The monolingual example uses listGlossaries() and deleteGlossary(), while the multilingual example uses listMultilingualGlossaries() and deleteMultilingualGlossary(). ```php // monolingual glossary example $glossaries = $deeplClient->listGlossaries(); foreach ($glossaries as $glossary) { if ($glossary->name === 'Old glossary') { $deeplClient->deleteGlossary($glossary); } } ``` ```php // multilingual glossary example $glossaries = $deeplClient->listMultilingualGlossaries(); foreach ($glossaries as $glossary) { if ($glossary->name === 'Old glossary') { $deeplClient->deleteMultilingualGlossary($glossary); } } ``` -------------------------------- ### createStyleRuleCustomInstruction: Method signature and usage example Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/api-reference/deeplclient.md Creates a custom instruction for a style rule. The method signature shows the parameters and return type. The usage example creates an instruction with label 'Formality', prompt 'Use formal language', and source language 'de'. Throws DeepLException for empty label or prompt. ```php public function createStyleRuleCustomInstruction( $styleRule, string $label, string $prompt, ?string $sourceLanguage = null ): CustomInstruction ``` ```php $instruction = $client->createStyleRuleCustomInstruction($rule, 'Formality', 'Use formal language', 'de'); ``` -------------------------------- ### Get a style rule with getStyleRule() Source: https://github.com/deepl/deepl-php/blob/main/README.md Retrieves a single style rule by its ID or a StyleRuleInfo object. The example prints the rule's name and language. ```php $styleRule = $deeplClient->getStyleRule('dca2e053-8ae5-45e6-a0d2-881156e7f4e4'); echo "{$styleRule->name} ({$styleRule->language})\n"; ``` -------------------------------- ### getAllStyleRules: Method signature and usage example Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/api-reference/deeplclient.md Retrieves all style rules with optional pagination and detail flags. The method signature shows the parameters and return type. The usage example calls the method with page 0, page size 25, and detailed true, then iterates over the returned StyleRuleInfo objects to print their names. ```php public function getAllStyleRules( ?int $page = null, ?int $pageSize = null, ?bool $detailed = null ): array ``` ```php $rules = $client->getAllStyleRules(0, 25, true); foreach ($rules as $rule) { echo $rule->name; } ``` -------------------------------- ### Instantiate DeepLClient with options Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/api-reference/deeplclient.md Example of creating a DeepLClient with a sample auth key and options for max_retries, timeout, and send_platform_info. The auth key shown is a placeholder. ```php $client = new \DeepL\DeepLClient('f63c02c5-f056-...', [ 'max_retries' => 5, 'timeout' => 10.0, 'send_platform_info' => false, ]); ``` -------------------------------- ### createStyleRule: Method signature and usage example Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/api-reference/deeplclient.md Creates a new style rule with a name, language, and optional configured rules and custom instructions. The method signature shows the parameters and return type. The usage example creates a rule named 'Formal English' for English with a configured rule for formal tone and a custom instruction for formality. ```php public function createStyleRule( string $name, string $language, ?array $configuredRules = null, ?array $customInstructions = null ): StyleRuleInfo ``` ```php $rule = $client->createStyleRule('Formal English', 'en', [ 'style_and_tone' => ['formal_informal' => 'formal'], ], [['label' => 'Formality', 'prompt' => 'Always use formal language']]); ``` -------------------------------- ### Get multilingual glossary example Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/api-reference/glossaries.md Example usage of the v3 client method getMultilingualGlossary. Retrieves a glossary by ID and accesses its name and the sourceLang of the first dictionary. ```php $glossary = $client->getMultilingualGlossary($glossaryId); echo $glossary->name . ' ' . $glossary->dictionaries[0]->sourceLang; ``` -------------------------------- ### Install deepl-php with Composer Source: https://github.com/deepl/deepl-php/blob/main/README.md Install the deepl-php library using Composer. Requires PHP 7.3 or later, but updating to PHP 8.1+ is recommended for continued support. ```shell composer require deeplcom/deepl-php ``` -------------------------------- ### Translation memories usage example Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/api-reference/deeplclient.md Example of listing translation memories and getting a specific translation memory by ID. ```php $tms = $client->listTranslationMemories(); $tm = $client->getTranslationMemory('a74d88fb-ed2a-4943-a664-a4512398b994'); ``` -------------------------------- ### Using getLanguageResources() Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/api-reference/language-resources.md Example of retrieving language resources from the client and iterating over features, dumping the needsSourceSupport property. ```php $resources = $client->getLanguageResources(); foreach ($resources as $resource) { echo $resource->resource; foreach ($resource->features as $name => $feature) { var_dump($feature->needsSourceSupport); // true/false/null } } ``` -------------------------------- ### Get glossary entries (monolingual and multilingual) Source: https://github.com/deepl/deepl-php/blob/main/upgrading_to_multilingual_glossaries.md Both examples retrieve glossary entries for an existing glossary. The monolingual version returns a GlossaryEntries object. The multilingual version requires specifying the source and target language codes and returns an array of MultilingualGlossaryDictionaryEntries; the first element's entries are printed. ```php // monolingual glossary example $glossaryId = '559192ed-8e23-...'; $entries = $deeplClient->getGlossaryEntries($myGlossary); print_r($entries->getEntries()); // Array ( [artist] => Maler, [prize] => Gewinn) ``` ```php // multilingual glossary example $glossaryId = '559192ed-8e23-...'; $glossaryDicts = $deeplClient->getMultilingualGlossaryEntries($myGlossary, "en", "de"); print_r($glossaryDicts[0]->getEntries()); // Array ( [artist] => Maler, [prize] => Gewinn) ``` -------------------------------- ### standardizeLanguageCode usage example Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/api-reference/languagecode.md Example call showing 'en-us' becomes 'en-US'. Outputs 'en-US'. ```php echo \DeepL\LanguageCode::standardizeLanguageCode('en-us'); // 'en-US' ``` -------------------------------- ### listTranslationMemorySegments usage example Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/api-reference/deeplclient.md Example of paginating through all segments of a translation memory using the cursor-based pagination. It loops until nextPageCursor is null, printing each segment's sourceText. ```php $cursor = null; do { $opts = ['page_size' => 50]; if ($cursor !== null) { $opts['page_cursor'] = $cursor; } $page = $client->listTranslationMemorySegments($tmId, $opts); foreach ($page->segments as $segment) { echo $segment->sourceText; } $cursor = $page->nextPageCursor; } while ($cursor !== null); ``` -------------------------------- ### Create multilingual glossary with dictionary entries Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/api-reference/glossaries.md Example of creating a MultilingualGlossaryDictionaryEntries instance and using it to create a multilingual glossary via the client. ```php $dict = new \DeepL\MultilingualGlossaryDictionaryEntries('en', 'de', \DeepL\GlossaryEntries::fromEntries(['artist' => 'Maler', 'prize' => 'Gewinn'])); $glossary = $client->createMultilingualGlossary('My glossary', [$dict]); ``` -------------------------------- ### Example using getLanguagesForResource with include constant Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/api-reference/language-resources.md Shows how to retrieve languages for the TRANSLATE_TEXT resource including beta languages, then iterate to find target languages that support formality, echoing their codes. ```php $languages = $client->getLanguagesForResource(\DeepL\LanguageResource::RESOURCE_TRANSLATE_TEXT, [\DeepL\LanguageSupport::INCLUDE_BETA]); foreach ($languages as $lang) { if ($lang->usableAsTarget && $lang->supports(\DeepL\LanguageFeature::FEATURE_FORMALITY)) { echo $lang->code; // formality-capable target language } } ``` -------------------------------- ### createStyleRule with ConfiguredRules example Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/api-reference/stylerules.md Shows how to pass a configured rules array to `DeepLClient::createStyleRule`. Uses snake_case wire keys, e.g., `style_and_tone` and `punctuation`. ```php $client->createStyleRule('Formal English', 'en', [ 'style_and_tone' => ['formal_informal' => 'formal'], 'punctuation' => ['oxford_comma' => 'always'], ]); ``` -------------------------------- ### DeepL\Translator instantiation example Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/api-reference/translator.md Creates a new translator with an authentication key and a timeout option. The auth key must be a non-empty string; otherwise a DeepLException is thrown. ```php $translator = new \DeepL\Translator('f63c02c5-f056-...', ['timeout' => 15.0]); ``` -------------------------------- ### GET /v2/glossaries Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/endpoints.md Lists all glossaries. ```APIDOC ## GET /v2/glossaries ### Description Lists all glossaries. ### Method GET ### Endpoint /v2/glossaries ### Response #### Success Response (200) - **glossaries** (array) - List of glossary info objects. #### Response Example { "glossaries": [ { "glossary_id": "string", "name": "string", "source_lang": "string", "target_lang": "string", "entries_format": "tsv" } ] } ``` -------------------------------- ### translateDocument with document minification Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/api-reference/translator.md Example enabling document minification via the ENABLE_DOCUMENT_MINIFICATION option to reduce media size before translation. ```php $translator->translateDocument($inFile, $outFile, 'en', 'de', [\DeepL\TranslateDocumentOptions::ENABLE_DOCUMENT_MINIFICATION => true]); ``` -------------------------------- ### GET {download_url} Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/endpoints.md Downloads a translation memory export from a pre-signed storage URL. ```APIDOC ## GET {download_url} ### Description Downloads a translation memory export from a pre-signed storage URL. ### Method GET ### Endpoint {download_url} ### Notes - The response is streamed to an output file. - The request is sent via a storage client without an authentication header. ``` -------------------------------- ### Manage custom instructions for a style rule Source: https://github.com/deepl/deepl-php/blob/main/README.md Manages custom instructions for a style rule: create, get, update, and delete. The create method accepts an optional source language (e.g., 'de'). ```php // Create a custom instruction $instruction = $deeplClient->createStyleRuleCustomInstruction( $styleRule, 'Formality', 'Always use formal language', 'de' // optional source language ); echo "Instruction ID: {$instruction->id}\n"; // Get a custom instruction $instruction = $deeplClient->getStyleRuleCustomInstruction($styleRule, $instruction->id); // Update a custom instruction $updated = $deeplClient->updateStyleRuleCustomInstruction( $styleRule, $instruction->id, 'Updated Label', 'Use very formal language' ); // Delete a custom instruction $deeplClient->deleteStyleRuleCustomInstruction($styleRule, $instruction->id); ``` -------------------------------- ### Create glossary with GlossaryEntries::fromEntries() Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/api-reference/glossaries.md Creates a v2 glossary using an associative array of source-to-target terms. The example passes the entries to Translator::createGlossary() with a name and language codes. ```php $entries = \DeepL\GlossaryEntries::fromEntries(['artist' => 'Maler', 'prize' => 'Gewinn']); $client->createGlossary('My glossary', 'en', 'de', $entries); ``` -------------------------------- ### GET /v2/usage Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/endpoints.md Retrieves usage information for the current account. ```APIDOC ## GET /v2/usage ### Description Retrieves usage information for the current account. ### Method GET ### Endpoint /v2/usage ### Response #### Success Response (200) - **character_count** (integer) - Number of characters used. - **character_limit** (integer) - Character limit. - **document_count** (integer) - Number of documents used. - **document_limit** (integer) - Document limit. - **team_document_count** (integer) - Team document count. - **team_document_limit** (integer) - Team document limit. #### Response Example { "character_count": 123, "character_limit": 500000, "document_count": 1, "document_limit": 10, "team_document_count": 0, "team_document_limit": 0 } ``` -------------------------------- ### StyleRuleInfo Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/api-reference/stylerules.md Documentation for the StyleRuleInfo data class, including its constructor, static methods, and usage examples. ```APIDOC ## StyleRuleInfo ### Description Data class representing information about a style rule list. Returned by `DeepLClient::createStyleRule()`, `getStyleRule()`, `getAllStyleRules()`, `updateStyleRuleName()`, `updateStyleRuleConfiguredRules()`. ### Constructor ```php public function __construct( string $styleId, string $name, DateTime $creationTime, DateTime $updatedTime, string $language, int $version, ?ConfiguredRules $configuredRules = null, ?array $customInstructions = null ) ``` ### Parameters - **styleId** (string) - Required - Unique ID assigned to the style rule list - **name** (string) - Required - User-defined name - **creationTime** (DateTime) - Required - Creation timestamp - **updatedTime** (DateTime) - Required - Last-updated timestamp - **language** (string) - Required - Language code of the style rule list - **version** (int) - Required - Version number - **configuredRules** (?ConfiguredRules) - Optional - Default: `null` - The predefined rules that have been enabled - **customInstructions** (?array) - Optional - Default: `null` - `CustomInstruction[]`, or null ### Static Methods #### getStyleId ```php public static function getStyleId($styleRule): string ``` Accepts `string|StyleRuleInfo`; strings returned as-is — used by all style-rule client methods. #### fromJson ```php public static function fromJson(array $json): StyleRuleInfo ``` #### parseList ```php public static function parseList(string $content): array ``` Returns `StyleRuleInfo[]` from `{'style_rules': [...]}`. ### Methods #### __toString ```php __toString(): string ``` Returns `"StyleRule \"{name}\" ({styleId})"`. ### Usage Example ```php $rule = $client->getStyleRule('dca2e053-8ae5-45e6-a0d2-881156e7f4e4'); echo "{$rule->name} ({$rule->language}) v{$rule->version}"; if ($rule->configuredRules && $rule->configuredRules->punctuation) { echo 'Has punctuation rules'; } ``` A `StyleRuleInfo` may be passed directly as the `style_id` translation option: ```php $result = $client->translateText('Hello, world!', 'en', 'de', ['style_id' => $rule]); // equivalent to ['style_id' => $rule->styleId] ``` ``` -------------------------------- ### Create and download translation memory export Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/api-reference/translation-memories.md Example of creating a translation memory export, checking whether it reused an existing export, waiting for the job to complete, and downloading the exported TMX file. ```php $created = $client->createTranslationMemoryExport($tmId); echo $created->reusedExisting ? "Reused an existing export\n" : "Started a new export\n"; $job = $client->waitUntilTranslationMemoryJobDone($created->jobId); $client->downloadTranslationMemoryExport($job, '/path/to/exported.tmx'); ``` -------------------------------- ### getStyleRuleCustomInstruction: Method signature Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/api-reference/deeplclient.md Retrieves a custom instruction from a style rule. Uses the GET /v3/style_rules/{style_id}/custom_instructions/{instruction_id} endpoint. ```php public function getStyleRuleCustomInstruction($styleRule, string $instructionId): CustomInstruction ``` -------------------------------- ### Passing AppInfo to DeepLClient Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/api-reference/results.md Example of passing an AppInfo instance via the 'app_info' constructor option to DeepLClient. The app name and version are appended to the User-Agent as {appName}/{appVersion}. ```php $client = new \DeepL\DeepLClient('YOUR_AUTH_KEY', ['app_info' => new \DeepL\AppInfo('my-custom-php-chat-client', '1.2.3')]); ``` -------------------------------- ### GET /v3/style_rules/{id}/custom_instructions/{cid} Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/endpoints.md Retrieves a specific custom instruction for a style rule. ```APIDOC ## GET /v3/style_rules/{id}/custom_instructions/{cid} ### Description Retrieves a specific custom instruction for a style rule. ### Method GET ### Endpoint /v3/style_rules/{id}/custom_instructions/{cid} ### Parameters #### Path Parameters - **id** (string) - Required - The style rule ID. - **cid** (string) - Required - The custom instruction ID. ### Response #### Success Response (200) - **CustomInstruction** (object) - The requested custom instruction. #### Response Example { "id": "string", "label": "string", "prompt": "string" } ``` -------------------------------- ### exportTranslationMemoryToFilepath - One-shot export Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/api-reference/deeplclient.md Creates a job, waits for completion, and downloads the TMX to the output file. Returns the completed job. The example shows the call and printing the download URL. ```php $job = $client->exportTranslationMemoryToFilepath($tmId, '/path/to/exported.tmx', 300); echo $job->result()->downloadUrl; ``` -------------------------------- ### Get a translation memory with getTranslationMemory() Source: https://github.com/deepl/deepl-php/blob/main/README.md Retrieves a single translation memory by its ID or a `TranslationMemoryInfo` object. The example checks `updatedTime` for null before formatting it with `DateTime::ATOM`. ```php $tm = $deeplClient->getTranslationMemory('a74d88fb-ed2a-4943-a664-a4512398b994'); echo "{$tm->name}: {$tm->segmentCount} segments\n"; if ($tm->updatedTime !== null) { echo " Updated: " . $tm->updatedTime->format(DateTime::ATOM) . "\n"; } ``` -------------------------------- ### TextResult usage example Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/api-reference/results.md Example of using TextResult from translateText, accessing text and billedCharacters properties. ```php $result = $client->translateText('Hello, world!', null, 'fr'); echo $result->text; // 'Bonjour, le monde !' echo $result->billedCharacters; // 13 ``` -------------------------------- ### CustomInstruction::__construct Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/api-reference/stylerules.md Creates a new CustomInstruction instance with a label, prompt, optional source language, and optional ID. ```APIDOC ## CustomInstruction::__construct ### Description Creates a new CustomInstruction instance. ### Method Constructor ### Signature ```php public function __construct( string $label, string $prompt, ?string $sourceLanguage = null, ?string $id = null ) ``` ### Parameters - **label** (string) - Required - Label for the custom instruction - **prompt** (string) - Required - Prompt text - **sourceLanguage** (?string) - Optional - Optional source language code - **id** (?string) - Optional - Unique ID assigned by the server ### Properties - `id` - string|null - `label` - string - `prompt` - string - `sourceLanguage` - string|null ``` -------------------------------- ### Constructor: __construct(string $authKey, array $options = []) Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/api-reference/translator.md Creates a new Translator instance with the given authentication key and optional configuration options. Validates the key, selects the appropriate server URL, and sets up HTTP clients for API and storage requests. ```APIDOC ## Constructor: __construct(string $authKey, array $options = []) ### Description Creates a new Translator instance with the given authentication key and optional configuration options. Validates the key, selects the appropriate server URL, and sets up HTTP clients for API and storage requests. ### Method Constructor ### Signature ```php public function __construct(string $authKey, array $options = []) ``` ### Parameters - **authKey** (string) - Required - DeepL authentication key - **options** (array) - Optional - TranslatorOptions keys, see options.md and configuration.md ### Behavior - Throws `DeepLException` if `authKey` is empty or `server_url` option is not a non-empty string. - Validates options via `TranslatorOptions::isValid($options)` (currently only logs warnings). - Selects server URL: `server_url` option, else `https://api-free.deepl.com` if key ends in `:fx`, else `https://api.deepl.com`. - Strips trailing `/` from URL. - Builds default `Authorization: DeepL-Auth-Key $authKey` and `User-Agent` headers (overridable via `headers` option). - Constructs a `HttpClientWrapper` for API requests and a second credential-free `HttpClientWrapper` for pre-signed storage URLs. ### Example ```php $translator = new \DeepL\Translator('f63c02c5-f056-...', ['timeout' => 15.0]); ``` ### Throws - `DeepLException` if `authKey` is empty or `server_url` option is not a non-empty string. ``` -------------------------------- ### POST /v3/style_rules/{id}/custom_instructions Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/endpoints.md Creates a custom instruction for a style rule. ```APIDOC ## POST /v3/style_rules/{id}/custom_instructions ### Description Creates a custom instruction for a style rule. ### Method POST ### Endpoint /v3/style_rules/{id}/custom_instructions ### Parameters #### Path Parameters - **id** (string) - Required - The style rule ID. #### Request Body - **label** (string) - Required - Label for the custom instruction. - **prompt** (string) - Required - Prompt text. - **source_language** (string) - Optional - Source language code. ### Response #### Success Response (200) - **CustomInstruction** (object) - The created custom instruction. #### Response Example { "id": "string", "label": "string", "prompt": "string" } ``` -------------------------------- ### removeRegionalVariant usage example Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/api-reference/languagecode.md Example call showing 'pt-BR' becomes 'pt'. Outputs 'pt'. Used internally by v2 glossary creation methods, which support only base language types. ```php echo \DeepL\LanguageCode::removeRegionalVariant('pt-BR'); // 'pt' ``` -------------------------------- ### Create, update, and delete custom instruction Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/api-reference/stylerules.md Shows how to create, update, and delete a custom instruction for a style rule. The create method returns an instruction with an id, which is used for updates and deletions. ```php $instruction = $client->createStyleRuleCustomInstruction($rule, 'Formality', 'Always use formal language', 'de'); echo "Instruction ID: {$instruction->id}"; $updated = $client->updateStyleRuleCustomInstruction($rule, $instruction->id, 'Updated Label', 'Use very formal language'); $client->deleteStyleRuleCustomInstruction($rule, $instruction->id); ``` -------------------------------- ### GET /v3/glossaries Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/endpoints.md Lists all multilingual glossaries. ```APIDOC ## GET /v3/glossaries ### Description Lists all multilingual glossaries. ### Method GET ### Endpoint /v3/glossaries ### Response #### Success Response (200) - **glossaries** (array) - List of glossaries. - **id** (string) - Glossary ID. - **name** (string) - Glossary name. - **dictionaries** (array) - List of dictionaries. #### Response Example { "glossaries": [ { "id": "123", "name": "My Glossary", "dictionaries": [] } ] } ``` -------------------------------- ### GET /v3/languages/resources Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/endpoints.md Retrieves available language resources. ```APIDOC ## GET /v3/languages/resources ### Description Retrieves available language resources. ### Method GET ### Endpoint /v3/languages/resources ### Response #### Success Response (200) - **array** (array) - List of LanguageResource objects. #### Response Example [] ``` -------------------------------- ### Configure DeepLClient with TranslatorOptions Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/configuration.md Shows how to pass configuration options as the second constructor argument to DeepLClient. The options array uses TranslatorOptions constants for max_retries, timeout, and app_info. Unknown option keys only produce a warning via the configured logger. ```php $options = [ \DeepL\TranslatorOptions::MAX_RETRIES => 5, \DeepL\TranslatorOptions::TIMEOUT => 10.0, \DeepL\TranslatorOptions::APP_INFO => new \DeepL\AppInfo('my-custom-php-chat-client', '1.2.3'), ]; $client = new \DeepL\DeepLClient('YOUR_AUTH_KEY', $options); ``` -------------------------------- ### Constructor options — array accepted by Translator::__construct() / DeepLClient::__construct() Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/types.md Options for configuring the DeepL client or translator instance, including server URL, headers, timeout, retries, proxy, logger, HTTP client, platform info, and app info. ```APIDOC ## Constructor Options ### Description Options for configuring the DeepL client or translator instance. ### Accepted By - `Translator::__construct` - `DeepLClient::__construct` ### Constants - `TranslatorOptions::SERVER_URL` - `TranslatorOptions::HEADERS` - `TranslatorOptions::TIMEOUT` - `TranslatorOptions::MAX_RETRIES` - `TranslatorOptions::PROXY` - `TranslatorOptions::LOGGER` - `TranslatorOptions::HTTP_CLIENT` - `TranslatorOptions::SEND_PLATFORM_INFO` - `TranslatorOptions::APP_INFO` ### Options - **server_url** (?string) - Optional - Default: 'https://api.deepl.com' | 'https://api-free.deepl.com' - **headers** (?array) - Optional - Additional headers - **timeout** (?float) - Optional - Default: 10.0 - **max_retries** (?int) - Optional - Default: 5 - **proxy** (?string) - Optional - Proxy URL - **logger** (?Psr\Log\LoggerInterface) - Optional - PSR-3 logger - **http_client** (?Psr\Http\Client\ClientInterface) - Optional - PSR-18 HTTP client - **send_platform_info** (?bool) - Optional - Default: true - **app_info** (?DeepL\AppInfo) - Optional - App information ``` -------------------------------- ### GET /v2/glossaries/{id} Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/endpoints.md Retrieves a glossary by ID. ```APIDOC ## GET /v2/glossaries/{id} ### Description Retrieves a glossary by ID. ### Method GET ### Endpoint /v2/glossaries/{id} ### Parameters #### Path Parameters - **id** (string) - Required - Glossary ID. ### Response #### Success Response (200) - **glossary_id** (string) - Glossary ID. - **name** (string) - Glossary name. - **source_lang** (string) - Source language. - **target_lang** (string) - Target language. - **entries_format** (string) - Entries format. #### Response Example { "glossary_id": "string", "name": "string", "source_lang": "string", "target_lang": "string", "entries_format": "tsv" } ``` -------------------------------- ### GET /v3/translation_memories Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/endpoints.md Lists translation memories with optional pagination. ```APIDOC ## GET /v3/translation_memories?page=&page_size= ### Description Lists translation memories with optional pagination. ### Method GET ### Endpoint /v3/translation_memories ### Parameters #### Query Parameters - **page** (integer) - Optional - Page number for pagination. - **page_size** (integer) - Optional - Number of items per page. ### Response #### Success Response (200) - **translation_memories** (array) - List of translation memory objects. #### Response Example { "translation_memories": [] } ``` -------------------------------- ### GET /v3/glossaries/{id} Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/endpoints.md Retrieves a multilingual glossary by ID. ```APIDOC ## GET /v3/glossaries/{id} ### Description Retrieves a multilingual glossary by ID. ### Method GET ### Endpoint /v3/glossaries/{id} ### Parameters #### Path Parameters - **id** (string) - Required - Glossary ID. ### Response #### Success Response (200) - **id** (string) - Glossary ID. - **name** (string) - Glossary name. - **dictionaries** (array) - List of dictionaries. #### Response Example { "id": "123", "name": "My Glossary", "dictionaries": [] } ``` -------------------------------- ### Create a style rule with createStyleRule() Source: https://github.com/deepl/deepl-php/blob/main/README.md Creates a new style rule with a name and language. Optionally accepts configured rules and custom instructions. The response includes the rule's name and styleId. ```php // Create a basic style rule $styleRule = $deeplClient->createStyleRule('My Style', 'en'); echo "Created: {$styleRule->name} ({$styleRule->styleId})\n"; // Create with configured rules and custom instructions $styleRule = $deeplClient->createStyleRule( 'Formal English', 'en', ['style_and_tone' => ['formal_informal' => 'formal']], [['label' => 'Formality', 'prompt' => 'Always use formal language']] ); ``` -------------------------------- ### GET /v3/translation_memories/jobs/{job_id} Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/endpoints.md Retrieves the status of a translation memory job. ```APIDOC ## GET /v3/translation_memories/jobs/{job_id} ### Description Retrieves the status of a translation memory job. ### Method GET ### Endpoint /v3/translation_memories/jobs/{job_id} ### Parameters #### Path Parameters - **job_id** (string) - Required - The ID of the job. ### Response #### Success Response (200) - **TranslationMemoryJob** (object) - The job details. ``` -------------------------------- ### CustomInstruction constructor Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/api-reference/stylerules.md Constructor for CustomInstruction. Requires a label and prompt; optional source language and ID. The ID is assigned by the server. ```php public function __construct( string $label, string $prompt, ?string $sourceLanguage = null, ?string $id = null ) ``` -------------------------------- ### GET /v3/translation_memories/{id} Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/endpoints.md Retrieves a specific translation memory by its ID. ```APIDOC ## GET /v3/translation_memories/{id} ### Description Retrieves a specific translation memory by its ID. ### Method GET ### Endpoint /v3/translation_memories/{id} ### Parameters #### Path Parameters - **id** (string) - Required - The ID of the translation memory. ### Response #### Success Response (200) - **TranslationMemoryInfo** (object) - The translation memory details. ``` -------------------------------- ### GET /v3/style_rules/{id} Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/endpoints.md Retrieves a specific style rule by its ID. ```APIDOC ## GET /v3/style_rules/{id} ### Description Retrieves a specific style rule by its ID. ### Method GET ### Endpoint /v3/style_rules/{id} ### Parameters #### Path Parameters - **id** (string) - Required - The style rule ID. ### Response #### Success Response (200) - **StyleRuleInfo** (object) - The requested style rule. #### Response Example { "id": "string", "name": "string", "language": "string" } ``` -------------------------------- ### List all style rules with getAllStyleRules() Source: https://github.com/deepl/deepl-php/blob/main/README.md Lists all stored style rules. The method accepts optional parameters: page (0-indexed), pageSize, and detailed. The detailed example prints number formatting rules when present. ```php // Get all style rules $styleRules = $deeplClient->getAllStyleRules(); foreach ($styleRules as $rule) { echo "{$rule->name} ({$rule->styleId})\n"; } // Get style rules with detailed configuration $styleRulesDetailed = $deeplClient->getAllStyleRules(detailed: true); foreach ($styleRulesDetailed as $rule) { if ($rule->configuredRules && $rule->configuredRules->numbers) { echo " Number formatting: " . implode(", ", array_keys($rule->configuredRules->numbers)) . "\n"; } } ``` -------------------------------- ### GET /v2/glossary-language-pairs Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/endpoints.md Retrieves the list of supported glossary language pairs. ```APIDOC ## GET /v2/glossary-language-pairs ### Description Retrieves the list of supported glossary language pairs. ### Method GET ### Endpoint /v2/glossary-language-pairs ### Response #### Success Response (200) - **supported_languages** (array) - List of language pairs. - **source_lang** (string) - Source language code. - **target_lang** (string) - Target language code. #### Response Example { "supported_languages": [ { "source_lang": "en", "target_lang": "de" } ] } ``` -------------------------------- ### GET /v2/languages?type=target Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/endpoints.md Retrieves the list of supported target languages. ```APIDOC ## GET /v2/languages?type=target ### Description Retrieves the list of supported target languages. ### Method GET ### Endpoint /v2/languages?type=target ### Parameters #### Query Parameters - **type** (string) - Required - Set to 'target'. ### Response #### Success Response (200) - **name** (string) - Language name. - **language** (string) - Language code. - **supports_formality** (boolean) - Whether the language supports formality. #### Response Example [ { "name": "German", "language": "de", "supports_formality": true } ] ``` -------------------------------- ### Constructor Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/api-reference/deeplclient.md Creates a new DeepLClient instance with the provided authentication key and optional configuration options. The constructor is lazy and does not connect to the API. ```APIDOC ## Constructor ### Description Creates a new DeepLClient instance with the provided authentication key and optional configuration options. The constructor is lazy and does not connect to the API. ### Method Constructor ### Signature ```php public function __construct(string $authKey, array $options = []) ``` ### Parameters - **authKey** (string) - Required - DeepL API authentication key as found in your DeepL Pro account - **options** (array) - Optional - Associative array of `TranslatorOptions`/`DeepLClientOptions` keys — see [configuration.md](../configuration.md) ### Returns A fully constructed client. Construction is lazy; it does **not** connect to the API and returns immediately. ### Throws - `DeepLException` if `authKey` is an empty string (`'authKey must be a non-empty string'`), or if the `server_url` option is not a non-empty string. ### Example ```php $client = new \DeepL\DeepLClient('f63c02c5-f056-...', [ 'max_retries' => 5, 'timeout' => 10.0, 'send_platform_info' => false, ]); ``` ``` -------------------------------- ### GET /v2/languages?type=source Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/endpoints.md Retrieves the list of supported source languages. ```APIDOC ## GET /v2/languages?type=source ### Description Retrieves the list of supported source languages. ### Method GET ### Endpoint /v2/languages?type=source ### Parameters #### Query Parameters - **type** (string) - Required - Set to 'source'. ### Response #### Success Response (200) - **name** (string) - Language name. - **language** (string) - Language code. - **supports_formality** (boolean) - Optional - Whether the language supports formality. #### Response Example [ { "name": "English", "language": "en", "supports_formality": true } ] ``` -------------------------------- ### GET /v3/languages Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/endpoints.md Retrieves languages for a given resource, with optional include parameters. ```APIDOC ## GET /v3/languages ### Description Retrieves languages for a given resource, with optional include parameters. ### Method GET ### Endpoint /v3/languages ### Parameters #### Query Parameters - **resource** (string) - Required - The resource type. - **include** (string) - Optional - Can be repeated for each value. ### Response #### Success Response (200) - **array** (array) - List of LanguageSupport objects. #### Response Example [] ``` -------------------------------- ### AppInfo constructor Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/api-reference/results.md Constructor for the AppInfo class. Requires the application name and version as strings. These are used to identify the host application in requests. ```php public function __construct(string $appName, string $appVersion) ``` -------------------------------- ### GET /v2/glossaries/{id}/entries Source: https://github.com/deepl/deepl-php/blob/main/_autodocs/endpoints.md Retrieves glossary entries in TSV format. ```APIDOC ## GET /v2/glossaries/{id}/entries ### Description Retrieves the entries of a glossary in TSV format. ### Method GET ### Endpoint /v2/glossaries/{id}/entries ### Parameters #### Path Parameters - **id** (string) - Required - Glossary ID. ### Response #### Success Response (200) - **body** (string) - TSV entries. #### Response Example TSV content ```