CDT Client is a library offering a PHP API to consume Translation Centre For the Bodies of the EU services.
Use Composer to install the package:
$ composer require openeuropa/cdt-client
All calls should be done by instantiating the client class:
require_once 'vendor/autoload.php';
$client = new \OpenEuropa\CdtClient\ApiClient(
new \GuzzleHttp\Client(),
new \GuzzleHttp\Psr7\HttpFactory(),
new \GuzzleHttp\Psr7\HttpFactory(),
[
// For a full list of options see "Configuration".
'apiBaseUrl' => 'https://example.com',
'username' => 'your-user-name',
'password' => 'your-password',
'client' => 'client-name',
]
);
In the above example, we're passing the Guzzle HTTP client, request, stream and URI factories. But these can be replaced by any similar factories that are implementing the PSR interfaces. The last parameter is the configuration.
Possible configurations:
username
(string): Used for authentication.password
(string): Used for authentication.client
(string): Used for authentication.apiBaseUrl
(string, valid URI): The base API url.
$response = $client->checkConnection();
Will return true or false depending on the availability of the CDT service.
$response = $client->getReferenceData();
Will return an array of business reference data, serialized into OpenEuropa\CdtClient\Model\Response\ReferenceData
.
To validate and send a translation request, run the following code:
use OpenEuropa\CdtClient\Model\Request\Translation;
use OpenEuropa\CdtClient\Exception\ValidationErrorsException;
$translationRequest = new Translation()
try {
if ($client->validateTranslationRequest($translationRequest)) {
$correlationId = $client->sendTranslationRequest($translationRequest);
}
} catch (ValidationErrorsException $e) {
$errors = $e->getValidationErrors();
// Handle the errors.
}
On success, the sendTranslationRequest()
method will return the temporary Correlation ID.
$permanentId = $client->getPermanentIdentifier($correlationId);
Will return a permanent string identifier for the translation request, based on correlation ID. Throws the ValidationErrorsException
if the correlation ID is not found.
$translationStatus = $client->getRequestStatus($permanentId);
Will return information on the status of translation request, based on permanent ID. The information includes the list of files to download. Throws the ValidationErrorsException
if the permanent ID is invalid.
$file = $client->downloadFile($fileUrl);
Downloads the specified file, if available, based on the filename listed in getRequestStatus()
. Throws a ValidationErrorsException
if the filename is invalid.
Please read the full documentation for details on our code of conduct, and the process for submitting pull requests to us.
We use SemVer for versioning.