Google hosts a terrific Ajax language Application Programming Interface (API), which allows developers to integrate Google’s translate.google.com service into their own applications.

We don’t have to use the Ajax API in order to take advantage of this service. Google allow access to the API via url calls–supplied with GET or POST url variables. Please click here to view the developers guide for this process. Using this method we can access the translation service from any programming language capable of calling a url. In the example below we will use PHP.

Please find below the full code for the example which will be followed by a detailed explanation.

<?php
 
require_once "Curl.php";
 
$curl = new Curl("http://ajax.googleapis.com/ajax/services/language/translate");
$postData = array("v"=>"1.0","q"=>"hujambo u hali gani","langpair"=>"sw|en");
$curl->setPost($postData);
$curl->setReferer("http://blog.dingoeweb.com/");
$curl->createCurl();
 
$curl = json_decode($curl);
 
if( isset($curl->responseStatus) )
{
	if( $curl->responseStatus == "200" )
	{
		if( isset($curl->responseData) )
		{
			if( isset($curl->responseData->translatedText) )
			{
				echo $curl->responseData->translatedText;
			}
		}
	}
}
 
?>

First we use curl to call the google language API. PHP comes with a library called the ‘Client URL Library’ (CURL) which is capable of fetching webpages and returning the results. The various functions and configuration options for using the native CURL library with PHP can be found here. For this example we will use a convenient wrapper for PHP’s CURL library. It can be downloaded here (originally posted at php.net). Firstly we initialise a CURL object and add the language API’s url.

require_once "Curl.php";
 
$curl = new Curl("http://ajax.googleapis.com/ajax/services/language/translate");

Next we need to set up our url variables; we will be using the POST method when sending the request. The ‘v’ parameter tells the API we what version of the API to use–1.0 in our case. While the ‘q’ parameter indicates the text we wish to translate. For this example we are translating the Swedish text ‘hujambo u hali gani’ into English. Finally the ‘langpair’ parameter tells the API from what language we are translating and to which language delimited by the ‘|’ character. In this case ’sw’ (Swedish) to ‘en’ (English). A full list of country codes is available here.

$postData = array("v"=>"1.0","q"=>"hujambo u hali gani","langpair"=>"sw|en");
$curl->setPost($postData);

The method ’setReferer’ includes a referer header with our request and is used because Google indicate in their documentation that they would like developers to include this information when sending requests to the API. ‘createCurl’ executes our request.

$curl->setReferer("http://blog.dingoeweb.com/");
$curl->createCurl();

Responses from the language API are always returned as JSON objects. We can use PHP’s native ‘json_decode’ function in order to serialise the data into PHP objects.

{
    "responseData": 
    {	
    	"translatedText":"hello how are you"
    }, 
    "responseDetails": null, 
    "responseStatus": 200
}
$curl = json_decode($curl);

Finally we ensure that the request was successful by checking that the necessary properties are included in our object and that the response code from Google was 200 (OK) then we check to see if any translated text was returned. If all went well we will echo the translated text. In this example the Swedish text ‘hujambo u hali gani’ is translated to ‘hello how are you’ and printed to the screen.

if( isset($curl->responseStatus) )
{
	if( $curl->responseStatus == "200" )
	{
		if( isset($curl->responseData) )
		{
			if( isset($curl->responseData->translatedText) )
			{
				echo $curl->responseData->translatedText;
			}
		}
	}
}

This technique could also be used in Flash, Ruby, Python, ASP etc.

More on Google’s language API.

2 Responses to “Using PHP with Google’s Language API”

  1. Jose Manuel Mora Fallas says:

    I’ve been trying to use this example, but and downloaded it and the curl wrapper file (http://github.com/erikeldridge/curl/blob/master/curl.php), but when trying to run it php returns an error because can’t find the setReferer method, wich is not present in the curl wrapper file.

    Could you please tell me if you used another curl wrapper file, or if i’m wrong in somethig.

    Thanks for your help.

  2. admin says:

    Hi Jose,

    I did indeed use a different wrapper. It can be downloaded here (http://blog.dingoeweb.com/files/Curl.php.zip). Thanks for picking this up. I have updated the post in case someone else wants to try this as well.

    regards
    Ben