Using PHP with Google’s Language API

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.


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");

[in_article_ad]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://{DOMAIN_NAME}/");
$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);

[in_article_ad]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.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *