How to Implement a Simple Language Detection API in PHP
Language detection has a lot of practical benefits within any application. Our Language Detection API Is free-to-use and can automatically detect the following languages from text input:
Danish (DAN), German (DEU), English (ENG), French (FRA), Italian (ITA), Japanese (JPN), Korean (KOR), Dutch (NLD), Norwegian (NOR), Portuguese (POR), Russian (ROS), Spanish (SPA), Swedish (SWE), Chinese (ZHO)
To implement this API for free, first register a free account on our website (this provides a free-tier API key with a limit of 800 API calls per month) and then follow steps below to structure your API call with ready-to-run PHP code examples.
Before we get to the code, let’s first run the following command to install the client SDK:
composer require cloudmersive/cloudmersive_nlp_api_client
After that, let’s copy the PHP code examples below to structure our reqeust properly:
<?php
require_once(__DIR__ . '/vendor/autoload.php');
// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');
$apiInstance = new Swagger\Client\Api\LanguageDetectionApi(
new GuzzleHttp\Client(),
$config
);
$input = new \Swagger\Client\Model\LanguageDetectionRequest(); // \Swagger\Client\Model\LanguageDetectionRequest |
try {
$result = $apiInstance->languageDetectionGetLanguage($input);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling LanguageDetectionApi->languageDetectionGetLanguage: ', $e->getMessage(), PHP_EOL;
}
?>
After that, simply configure your text input & API key, and you’re ready to start detecting languages with ease.