PHP Google Translation API multiple text strings in one POST request -
according google , sitepoint, there possibilities translate multiple text strings in 1 request. when tried translate multiple strings, resulted in replacing first string last one.
$handle = curl_init(); if (false === $handle) throw new exception('failed initialize'); curl_setopt($handle, curlopt_url,'https://www.googleapis.com/language/translate/v2'); curl_setopt($handle, curlopt_returntransfer, 1); curl_setopt($handle, curlopt_ssl_verifypeer, false); curl_setopt($handle, curlopt_postfields, array('key'=> $apikey, 'q' => $heading, 'q' => $content, 'source' => $sl, 'target' => $hl)); curl_setopt($handle,curlopt_httpheader,array('x-http-method-override: get')); $response = curl_exec($handle); $responsedecoded = json_decode($response, true); $responsecode = curl_getinfo($handle, curlinfo_http_code); curl_close($handle); if($responsecode != 200) { header("http/1.0 404 not found"); include_once("errordocument/404.html"); exit(); }else{ $heading = $responsedecoded['data']['translations'][0]['translatedtext']; $content = $responsedecoded['data']['translations'][1]['translatedtext']; }
any ideas?
$handle = curl_init(); if (false === $handle) throw new exception('failed initialize'); curl_setopt($handle, curlopt_url,'https://www.googleapis.com/language/translate/v2'); curl_setopt($handle, curlopt_returntransfer, 1); curl_setopt($handle, curlopt_ssl_verifypeer, false); $data = array('key' => $apikey, 'q' => array($heading,$content), 'source' => $sl, 'target' => $hl); curl_setopt($handle, curlopt_postfields, preg_replace('/%5b(?:[0-9]|[1-9][0-9]+)%5d=/', '=', http_build_query($data))); curl_setopt($handle,curlopt_httpheader,array('x-http-method-override: get')); $response = curl_exec($handle); $responsedecoded = json_decode($response, true); $responsecode = curl_getinfo($handle, curlinfo_http_code); curl_close($handle); if($responsecode != 200) { header("http/1.0 404 not found"); include_once("errordocument/404.html"); echo 'fetching translation failed! server response code:' . $responsecode . '<br>'; echo 'error description: ' . $responsedecoded['error']['errors'][0]['message'] . '<br>'; echo 'please contact website administrator'; exit(); }else{ $heading = $responsedecoded['data']['translations'][0]['translatedtext']; $content = $responsedecoded['data']['translations'][1]['translatedtext']; }
this works me. found solution out there. hope in future.
Comments
Post a Comment