API Details: Advanced Text Translator AI API
Description: Shows how to use the translate() method to translate text into a specified language.
Code Snippet:
<?php
require 'vendor/autoload.php';
use SharpAPI\SharpApiService;
// Load environment variables
$apiKey = getenv('SHARP_API_KEY');
if (!$apiKey) {
throw new Exception("API key not found. Please set SHARP_API_KEY in your environment variables.");
}
// Initialize the SharpApiService
$sharpApi = new SharpApiService($apiKey);
$textToTranslate = "La montée du niveau de la mer menace d'engloutir les Maldives
où l'eau douce commence déjà à manquer, mais le nouveau président de l'archipel
de l'océan Indien refuse toute relocalisation de sa population à l'étranger.
Dans un entretien à l'AFP, le président Mohamed Muizzu, un ingénieur du génie
civil de 45 ans, formé au Royaume-Uni, promet au contraire un ambitieux
programme de réhabilitation des terres et de surélévation des îles,
que critiquent des organisations environnementales.";
$targetLanguage = 'English';
try {
$statusUrl = $sharpApi->translate($textToTranslate, $targetLanguage, 'Neutral', null);
$resultJob = $sharpApi->fetchResults($statusUrl);
echo "Translated Text Result:\n";
print_r(json_decode($resultJob->getResultJson(), true));
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}$resultJob->getResultJson() format example:
{
"data": {
"type": "api_job_result",
"id": "5de4887a-0dfd-49b6-8edb-9280e468c210",
"attributes": {
"status": "success",
"type": "content_translate",
"result": {
"content": "The rise in sea levels threatens to engulf the Maldives where fresh water is already starting to run out, but the new president of the Indian Ocean archipelago refuses any relocation of its population abroad. In an interview with AFP, President Mohamed Muizzu, a 45-year-old civil engineering graduate trained in the United Kingdom, instead promises an ambitious program of land rehabilitation and island elevation, which environmental organizations criticize.",
"to_language": "English",
"from_language": "French"
}
}
}
}