Code Examples
PHP
Standard language detection in PHP (GET):
// set API Access Key
$access_key = 'YOUR_ACCESS_KEY';
// set query text
$query = 'Hello my friend, how are you today?';
// Initialize CURL:
$ch = curl_init('http://api.languagelayer.com/detect?access_key='.$access_key.'&query='.$query.'');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Store the data:
$json = curl_exec($ch);
curl_close($ch);
// Decode JSON response and store array:
$detection_result = json_decode($json, true);Batch language detection in PHP (POST):
function languagelayer_batch($query_array) {
// set access key
$access_key = 'YOUR_ACCESS_KEY';
foreach($query_array as $value) { $fields_string .= 'query[]='.$value.'&'; }
rtrim($fields_string, '&');
// Initialize CURL:
$ch = curl_init('http://api.languagelayer.com/batch?access_key='.$access_key);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Store the data:
$json = curl_exec($ch);
curl_close($ch);
echo $json;
// Decode JSON response:
$detection_results = json_decode($json, true);
// Access and use your preferred validation result objects
return $detection_results;
}
// set query texts
$query_array = array('hey friend', 'hola amigo', 'hallo freund');
// store results array
$results_array = languagelayer_batch($query_array);JavaScript (jQuery.ajax)
Standard language detection (GET):
// set endpoint and your access key
var access_key = 'YOUR_ACCESS_KEY';
var query = 'Hello my friend, how are you?';
// AJAX call
$.ajax({
url: 'http://api.languagelayer.com/detect?access_key=' + access_key + '&query=' + encodeURIComponent(query),
dataType: 'jsonp',
success: function(json) {
// Access and use your preferred validation result objects
console.log(json.success);
}
});Please note: In this example, the use of JSONP is entirely optional.
Help / Support
Any issues with API Integration? Be sure to take a look at our Frequently Asked Questions to see if your question has already been answered.
Still need something clarified? Please get in touch with our support team at [email protected].