How to get and parse json answer from google translate

Shara picture Shara · Apr 26, 2012 · Viewed 12.3k times · Source

I want to translate word with ajax request to google translate If i use curl, it willl be like:

curl_init("http://translate.google.com/translate_a/t?client=t&text=hello&sl=en&tl=ru");

But i cant use server-side scripts with queries because they will be from same IP
But i cant use JSONP request too, because it's not allowed i think. Here's server response:

[[["привет","hello","privet",""]],[["",["Алло","Здравствуйте."]],["имя существительное",["приветствие","приветственный возглас","возглас удивления"]],["глагол",["здороваться","звать","окликать"]],["междометие",["привет","здравствуйте","алло"]]],"en",,[["привет",[5],1,0,1000,0,1,0]],[["hello",4,,,""],["hello",5,[["привет",1000,1,0],["Здравствуй",0,1,0],["Здравствуйте",0,1,0],["Hello",0,1,0]],[[0,5]],"hello"]],,,[["en"]],13]

And i cant load content to iframe

<iframe src="http://translate.google.com/translate_a/t?client=t&text=hello&sl=en&tl=ru"></iframe>

because response content type is "text/javascript" and browser downloads a file

PS. i cant decode content with php function json_decode() or with jquery.ajax because response json is corrupt. You can test it here http://json.parser.online.fr/

Give me an advice, please

Answer

puromonogatari picture puromonogatari · May 10, 2012

Try:

http://translate.google.com/translate_a/t?client=p

if the client parameter is anything other than 't' it returns valid JSON data. Of course, you could always transform the 't' return variable into a valid array or JSON but this is easier.