How to get and parse json answer from google translate
Asked Answered
K

2

7

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

Keeler answered 26/4, 2012 at 13:23 Comment(4)
suggestion: (a) buy a license for a translation service that allows you to use "the same IP". Or (b) don't do translations. The limits google places on the translation service are there for a reason. It seems you are looking for a way around those limits. Don't do that.Cherianne
oh, god. It's experiment, i dont want to buy million-query license for one-day experiment.Keeler
Why are you bringing God into it? If it's an experiment then you need not worry about the same ip issue.Cherianne
Cheeso, i told about technical problem. Not moral or ethical. And i can't solve it, can you?Keeler
S
13

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.

Scheck answered 10/5, 2012 at 3:45 Comment(4)
Thank you, puromonogatari! I made this with transform way, but thanks for first solutionKeeler
NP, it should be noted though that I had some trouble sending characters to it.... I couldn't find the reason but for some reason sending HTML entities works fine "&x281" for example, without the ";", try that if you have any encoding issues.Scheck
And it started using captchaMoshe
It seems this doesn't work anymore. Response is always an empty string now. May I please, ask how did you find out about this? ThanksStatued
S
2

Make sure to add header param "User-Agent" = "Mozilla/4.0". Google adds captcha if you do not

Simper answered 2/6, 2015 at 22:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.