카카오 번역 API 사용을 위한 PHP 클라이언트
카카오 번역 API 사용을 위한 PHP 클라이언트 코드이다. PHP 7.0 이상의 환경에서 사용할 수 있다.
카카오 번역 API : https://developers.kakao.com/docs/restapi/translation
<?php
/**
* @author chicpro <chicpro@gmail.com>
* @copyright (c) chicpro
* @link https://chicpro.dev
*/
namespace chicpro\KAKAO;
class TRANSLATE
{
protected $host;
protected $appKey;
protected $query;
protected $sourceLanguage;
protected $targetLanguage;
public function __construct(string $appKey = '')
{
$this->host = 'https://kapi.kakao.com/v1/translation/translate';
$this->appKey = $appKey;
}
public function setHost(string $host)
{
$this->host = $host;
}
public function setAppKey(string $appKey)
{
$this->appKey = $appKey;
}
public function setQuery(string $query)
{
$this->query = $query;
}
public function setSourceLanguage(string $source)
{
$this->sourceLanguage = $source;
}
public function setTargetLanguage(string $target)
{
$this->targetLanguage = $target;
}
public function sendRequest()
{
$headers = array(
'Authorization: KakaoAK ' . $this->appKey
);
$postData = [
'src_lang' => $this->sourceLanguage,
'target_lang' => $this->targetLanguage,
'query' => $this->query
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $this->host);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));
$json = curl_exec($ch);
if ($errno = curl_errno($ch)) {
$result = new \stdClass;
$result->errno = $errno;
$result->error = 'Curl error: ' . curl_error($ch);
} else {
$response = json_decode($json);
$result = $response;
}
return $result;
}
}카카오 번역 API 예제 코드는 아래와 같다. (more…)
