Access Google Translate API with PHP
Recently I wanted to utilize Google's translation API, so I came up with the following PHP script. Please note that I didn't build in any error handling, debugging code, security measures, or the full range of features of the Google translation API.. all of which you should probably do if you're going to use this in a public or production environment.
Create a new .php file on your web server, as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | // Basic request parameters: // s = source language // d = destination language // q = Text to be translated $s = $_REQUEST['s']; $d = $_REQUEST['d']; $lang_pair = urlencode($s.'|'.$d); $q = urlencode($_REQUEST['q']); // Google's API translator URL $url = "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=".$q."&langpair=".$lang_pair; // Make sure to set CURLOPT_REFERER because Google doesn't like if you leave the referrer out $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_REFERER, "http://www.yoursite.com/translate.php"); $body = curl_exec($ch); curl_close($ch); $json = json_decode($body, true); echo $json['responseData']['translatedText']; |
Then just load up http://www.yoursite.com/translate.php?s=en&d=es&q=Test and you should see "Prueba" (Spanish for "Test").
Related Posts
9 comments9 Comments so far
Leave a reply

[...] I mentioned in my last post (Access Google Translate API with PHP), it is fairly trivial to access the Google Translate API using PHP. I recently came across Micha [...]
Hello,
thanks for valuable piece of code.
One problem not solved (I am not a php guru): it works for small text (like maximum 20 words); for text larger than about 20 words the script return error
Fatal error: Cannot use string offset as an array in C:\xampp\htdocs\test\translate.php on line 23
//line 23 is the same as above
If you may help, I really appreciate.
@George Thanks for your input. I will investigate the error and see what I can come up with for you.
@George Could you supply your test string? I've successfully tested text as long as ~2500 characters (300+ words). That error seems to indicate a problem with the JSON response.
Hello,
Of course, I want to do this from begenning.
Is not a special string, I've just copy some longer text and put it in the link as a variable. If I put only the first phrase of the same text in variable, google translate it good, but if the variable is longer (as bellow), on my computer localhost this is the error(Fatal error: Cannot use string offset as an array in C:\xampp\htdocs\test\translate.php on line 23). It seems php cannot return the translated text because google return an array (maybe will work an implode function after the echo $json['responseData']['translatedText'];?).
http://www.firme.weburl.ro/translate.php/?s=en&d=es&q=Green+and+eco+friendly+initiatives+are+a+high+priority+for+many+industries+and+businesses+these+days.+There+are+those+who+point+to+the+use+of+digital+signage+as+the+green+alternative+to+the+more+traditional+paper+advertising+but+is+that+really+the+case?+According+to+the+EPA+in+the+US+greenhouse+gases+are+being+increased+by+the+incineration+of+trash,+50%+of+which+is+un-recycled+paper+products.+The+economic+downturn+is+even+taking+a+toll+on+the+recycling+industry,+and+most+companies+have+dropped+the+idea+of+using+recycled+paper+products+as+an+expensive+hassle.%3C/p%3E%3Cp%3ESo+digital+signage+definitely+has+that+issues+over+paper+advertising.+Using+digital+signage+to+convey+an+ever+changeable+series+of+advertisements+and+content+without+the+need+for+all+that+paper,+printing+ink,+and+human+effort+seems+to+make+as+much+fiscal+as+it+does+environmental+sense.+In+addition+to+cutting+down+on+paper+usage,+saving+both+trees+and+space+in+already+overused+landfills+as+well+as+the+disposal+of+obsolete+signage+that+is+no+longer+useful,+a+company+can+also+decrease+its+carbon+footprint+by+eliminating+the+need+for+trips+back+and+forth+to+a+physical+site+to+change+and+replace+paper+signage.+The+digital+displays+themselves+are+constantly+evolving.+In+both+LCD+and+Plasma+based+digital+signage+systems+are+being+created+using+lighter,+thinner+and+far+more+energy+efficient+technology+than+ever+before.+This+does+make+an+argument+perhaps+against+the+true+%22green%22+nature+of+the+digital+signage+industry,+as+the+old+technology+is+constantly+being+%22thrown+away%22+to+make+way+for+the+newer,+more+efficient+technology+as+it+becomes+available.+Many+companies+do+not+however+throw+away+this+expensive+technology,+rather+donating+it,+or+selling+it+on+to+other+consumers.+A+used+plasma+or+LCD+monitor,+as+long+as+it+still+functions,+is+almost+always+a+saleable+commodity,+meaning+that+in+effect+the+green+concept+of+recycling+is+still+being+utilized.+When+you+compare+the+two,+it+does+certainly+seem+that+the+use+of+digital+signage+for+advertising+purposes+is+the+more+environmentally+responsible+way+for+companies+to+get+their+message+across,+creating+yet+another+argument+in+favor+of+more+companies+moving+away+from+traditional+paper+based+advertising+to+the+vital,+vibrant+world+of+digital+signage,+which+after+all+has+not+only+the+potential+to+be+the+green+alternative,+but+is+also+far+more+effective+at+capturing+the+attention+of+the+average+consumer
Hi,
Does the API return the alternative translations of words as in Google Translate itself? Or can you suggest a way to tackle the problem? I have tried many ways, excluding the use of the API, such as reading the whole web page and trying to find the tags containing the data i need, but they are empty!
Any help appreciated.
J
Hello;
Please how to translate text with more than 2500 chars?
My problem is like George. The error message is Fatal error: Cannot use string offset as an array in C:\AppServ\www\translate\TranslateCSV.php on line 23. But in other computer, the program can run. I'm using AppServ as Web Server.
@questor: You would have to make multiple requests to Google Translate and stitch the results together.