번역에 해당하는 글 2

PHP + Google Translate API 연동하기

Progmming/PHP|2018. 5. 4. 13:19
반응형

PHP + Google Translate API 연동하기



Google translate document site

https://cloud.google.com/translate/docs/?hl=ko



Google 에서 제공되는 샘플 소스

https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/translate


 

하지만 그냥 최소한의 코딩으로 처리를 하고자 했기 때문에..


php 함수를하나 만듬.


function translate($content) { $handle = curl_init(); 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' => "API Key", 'q' => $content, 'source' => "ko", 'target' => "en"); 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); return $responseDecoded['data']['translations'][0]['translatedText']; }


사용을 위해 신용카드 등록이 필요하지만 처음 등록 할 경우 300달러가 제공되니 참고해주세요.

댓글()

Google 웹사이트 번역기

Open API/Google|2016. 11. 30. 10:23
반응형

구글 플러그인을 이용해서 내 블로그 , 웹사이트에 번역기능을 추가하는 방법입니다.



사이트 등록 및 스크립트 가져오는 방법 


1. 등록 Url : https://translate.google.com/manager/website/add




2. 플러그인 설정하기 .


 



3. 스크립트 얻기.




4. 등록 후 화면




 




5. 적용 화면.


 

댓글()