translate a PHP $string using google translator API

前端 未结 5 1885
梦谈多话
梦谈多话 2020-12-08 17:26

been google\'ing for a while how is the best way to translate with google translator in PHP, found very different ways converting URLS, or using Js but i want to do it only

5条回答
  •  孤街浪徒
    2020-12-08 17:48

    I have new solution for this.. Because last solution need new version and some fetched other issue.


        $text = 'Test new message only.';
        $apiKey = '';
        $url = 'https://www.googleapis.com/language/translate/v2?key=' . $apiKey . '&q=' . rawurlencode($text) . '&source=en&target=fr';
        $handle = curl_init($url);
        curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($handle);
        $responseDecoded = json_decode($response, true);
    
        curl_close($handle);
    
        print_r($responseDecoded['data']['translations'][0]['translatedText']);
        die;
    
        //expected output
         Testez le nouveau message uniquement.
    

    I hope is very helpful in PHP

提交回复
热议问题