curl - translate a PHP array value using google translator API -
i working on translating text via google translate api.i have translate english language data comes database other language japanese , save different language output in database. right sending every string google translate api output in other language. takes long time , due multiple request limitation cannot translate whole data.
so question is, can translate whole array in single request using google translator api.
right using below code :
for($mn=0;$mn<count($languagefielddata);$mn++) { $field = $languagefielddata[$mn]['field']; $newval = $leadquery[0][$field]; if(!empty($newval)) { //$leadquery['ko'][0][$field] = translate($newval,'ko'); $leadquery['ja'][0][$field] = translate($newval,'ja'); //$leadquery['zh-cn'][0][$field] = translate($newval,'zh-cn'); } $newval = ""; } function curl($url,$params = array(),$is_coockie_set = false) { if(!$is_coockie_set) { $ckfile = tempnam ("/tmp", "curlcookie"); $ch = curl_init ($url); curl_setopt ($ch, curlopt_cookiejar, $ckfile); curl_setopt ($ch, curlopt_returntransfer, true); $output = curl_exec ($ch); } $str = ''; $str_arr= array(); foreach($params $key => $value) { $str_arr[] = urlencode($key)."=".urlencode($value); } if(!empty($str_arr)) $str = '?'.implode('&',$str_arr); $url = $url.$str; $ch = curl_init ($url); curl_setopt ($ch, curlopt_cookiefile, $ckfile); curl_setopt ($ch, curlopt_returntransfer, true); $output = curl_exec ($ch); return $output; } function translate($word,$conversion) { $word = urlencode($word); $url = 'http://translate.google.com/translate_a/t?client=t&text='.$word.'&hl=en&sl=en&tl='.$conversion.'&ie=utf-8&oe=utf-8&multires=1&otf=1&ssel=3&tsel=3&sc=1'; $name_en = curl($url); $name_en = explode('"',$name_en); return $name_en[1]; }
the short answer is: no, can't on single request. restful api [1] define methods receive string of characters.
but don't see point on needing shuch feature because can define own method encapsulates "hard work" of translating array of strings.
if want deal default quota limitation of 100 request/second/user can raise limit or add logic script no launch more x requests per second.
Comments
Post a Comment