curl – translate a PHP array value using google translator API-ThrowExceptions

Exception or error:

I am working on translating text via google translate api.I have to translate English Language data comes from database to other Language like Japanese and also save the different Language output in Database.
right now i am sending every string to google translate api to get the output in other language. But it takes very long time and due to multiple request limitation i cannot translate the whole data.

So my question is, can i translate the whole array in a single request using google translator API.

Right now i am using below code :

    $field = $languageFieldData[$mn]['field'];

    $newVal = $leadQuery[0][$field];
        //$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)
        $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 as $key => $value)
    $str_arr[] = urlencode($key)."=".urlencode($value);
    $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 = ''.$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];
How to solve:

The short answer is: No, you can’t get it on a single request. The RESTful API [1] only define methods to receive a string of characters.

But I don’t see the point on needing shuch feature because you can define your own method that encapsulates the “hard work” of translating an array of strings.

If you want to deal with the default quota limitation of 100 request/second/user you can always raise the limit or add some logic to your script to no launch more than X requests per second.



I resolved it. I converted array to html with the help of Div and its unique id.Google translator do not translate html tabs and attributes. It will only translate the content inside DIV.


you can also use implode with some UNIQUE character and then explode with the same character.

by using this we can send only one request to the API

Leave a Reply

Your email address will not be published. Required fields are marked *