How to decode Google translate response with php? How to decode Google translate response with php? json json

How to decode Google translate response with php?


To get the api to return JSON change the parameter

client=t

to

client=p


 //this will translate from en to ar -- you can change it for your needs function translate_word($en){    $file_content = file_get_contents('http://translate.google.com/translate_a/t?client=p&sl=auto&tl=ar&hl=en&sc=2&ie=UTF-8&oe=UTF-8&uptl=ar&oc=1&prev=conf&psl=auto&ptl=en&otf=1&it=sel.8936&ssel=0&tsel=3&q='.str_replace(" ","%20",$en));    $obj = json_decode($file_content);    if(!empty($obj->sentences[0]->trans)){        return $obj->sentences[0]->trans;    }else{        return $en;    }}//how to useecho translate_word("test translation process");//if you do not know short codes for your language you can just open 

translate.google.com and do described on screenshot

//you should have firebug extension installed on your browser

enter image description here

//if the answer was usefull please do not forget to vote up! thanks.


Try using PHP's json_decode function to convert that data to native PHP data types.