Search code examples
phpdetectiongoogle-language-api

What is the easiest way to use Google AJAX Language API to detect language with PHP?


There is an easy way to use the Google AJAX Language API to detect language with PHP without use any library or a giant framework?


Solution

  • Thats is easy,

    function detect_language($string) {
       $response = file_get_contents("http://www.google.com/uds/GlangDetect?v=1.0&q=" . urlencode($string));
       $response = json_decode($response, true);
       if ($response['responseStatus'] == 200) {
           return $response['responseData']['language'];
       } else {
           return "tw";
       }
    }