Download the PHP package benmorel/languagelayer without Composer
On this page you can find all versions of the php package benmorel/languagelayer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download benmorel/languagelayer
More information about benmorel/languagelayer
Files in benmorel/languagelayer
Package languagelayer
Short Description LanguageLayer API client for PHP
License MIT
Informations about the package languagelayer
LanguageLayer API client for PHP
Perform language detection in PHP using the LanguageLayer API.
Note: LanguageLayer is not being maintained anymore, and API calls started failing as of November 2020. This library is therefore archived.
Installation
This library is installable via Composer:
Requirements
This library requires PHP 7.1 or later.
Quickstart
You need a free API key from LanguageLayer to get started.
Just instantiate the LanguageLayer client, and start detecting:
The detectLanguages()
method returns an array of LanguageDetectionResult objects,
that you can inspect to decide what to do with each detected language.
Using https
You can use the API over a secure connection, if you have a paid plan that supports it:
Detecting a single language
As a convenience, the detectLanguage()
method helps you detect a single language from a text:
This method loops through the results to find a single reliable result. If it there is no reliable result, but the API
returned a single result, it will also accept it, unless the second parameter, $forceReliable
, is set to true:
If no single, acceptable result is found, a LanguageDetectionException is thrown.
Error handling
Any kind of error—an HTTP error, an error returned by the API, or any other kind of error related to this
library—throws a LanguageDetectionException
.
Therefore you should wrap all your detectLanguage()
and detectLanguages()
calls in a try
/catch
block:
If the exception was caused by an HTTP error, you can inspect the underlying Guzzle exception
by calling $exception->getPrevious()
if needed.
If the exception was caused by an error returned by the LanguageLayer API itself, you can inspect it,
in addition to the exception message, with $exception->getCode()
and $exception->getType().
You can, for example, act upon specific API errors:
Note: if the exception was not caused by an error returned by the API itself, getType()
will return null
.
See the LanguageLayer documentation for a list of error codes and types.