Skip to content

CHICPRO

  • Life Log
  • Cycling Log
  • Photo Log
    • Portrait
    • Landscape
    • Flower
    • Etc
  • Coding Log
  • Information

[PHP] Papago NMT API 이용을 위한 Class

2018-01-31 by 편리

네이버에서 제공하는 Papago NMT API 이용을 위한 Class 이다. 네이버 파파고 API는 https://developers.naver.com/docs/nmt/reference/ 를 참고한다. API 요청은 아래와 같은 코드로 실행하면 실행에 필요한 Class 코드는 포스트 하단에 있다. API 테스트 전 애플리케이션으 등록해야 하고 아래 코드에서 PAPAGO_NMT_ID, PAPAGO_NMT_SECRET 값을 설정해야 한다.

<?php
require './PapagoNMT.php';

define('PAPAGO_NMT_ID',     'client ID');
define('PAPAGO_NMT_SECRET', 'client Secret');

$papago = new PapagoNMT(PAPAGO_NMT_ID, PAPAGO_NMT_SECRET);

$papago->setSource('en');
$papago->setTarget('ko');

$papago->setText('hello');

$json = $papago->sendRequest();
$result = json_decode($json);

echo 'Translated : '.$result->message->result->translatedText;

PapagoNMT.php 파일의 코드이다.

<?php
/**
 * Author : chicpro (https://chicpro.dev)
 */

class PapagoNMT
{
    protected $clientID;
    protected $clientSecret;

    protected $requestURL = 'https://openapi.naver.com/v1/papago/n2mt';

    protected $source;
    protected $target;
    protected $encText;

    public function __construct($clientID, $clientSecret)
    {
        $this->clientID     = $clientID;
        $this->clientSecret = $clientSecret;
    }

    public function setSource($source)
    {
        $this->source = $source;
    }

    public function setTarget($target)
    {
        $this->target = $target;
    }

    public function setText($text)
    {
        $this->encText = trim($text);
    }

    public function sendRequest()
    {
        $headers = array();
        $headers[] = "Content-Type: application/x-www-form-urlencoded; charset=UTF-8";
        $headers[] = "X-Naver-Client-Id: ".$this->clientID;
        $headers[] = "X-Naver-Client-Secret: ".$this->clientSecret;

        $postvars = array(
            'source' => $this->source,
            'target' => $this->target,
            'text'   => $this->encText
        );
        
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->requestURL);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postvars));
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);        
        
        $response = curl_exec ($ch);
        
        $err = curl_error($ch);

        curl_close($ch);

        if($err)
            return $err;

        return $response;
    }
}

Post navigation

Previous Post:

[PHP] XMLRPC를 이용한 WordPress 포스트 등록

Next Post:

nginx에서 특정 IP에서만 xmlrpc.php 파일에 접근하도록 설정

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Recent Posts

  • SK 세븐모바일 유심 셀프교체
  • php 배열 연산에서 + 와 array_merge 의 차이
  • pcntl_fork 를 이용한 다중 프로세스 실행
  • 아이폰 단축어를 이용하여 주중 공휴일엔 알람 울리지 않게 하기
  • 구글 캘린더 전체일정 재동기화
  • OpenLiteSpeed 웹서버에 HTTP 인증 적용
  • OpenLiteSpeed 웹어드민 도메인 연결
  • WireGuard를 이용한 VPN 환경 구축
  • Ubuntu 22.04 서버에 OpenLiteSpeed 웹서버 세팅
  • 맥 vim 세팅

Recent Comments

  • 편리 on 업무관리용 그누보드 게시판 스킨
  • 임종섭 on 업무관리용 그누보드 게시판 스킨
  • 캐논 5D 펌웨어 | Dslr 펌웨어 업그레이드 방법 82 개의 베스트 답변 on 캐논 EOS 30D 펌웨어 Ver 1.0.6 , EOS 5D 펌웨어 Ver 1.1.1
  • Top 5 캐논 5D 펌웨어 Top 89 Best Answers on 캐논 EOS 30D 펌웨어 Ver 1.0.6 , EOS 5D 펌웨어 Ver 1.1.1
  • 편리 on 워드프레스 애니메이션 gif 파일을 mp4로 변환하여 출력하기
  • 임팀장 on 워드프레스 애니메이션 gif 파일을 mp4로 변환하여 출력하기
  • 편리 on Notepad++ NppFTP 플러그인 수동 설치
  • paul-j on Notepad++ NppFTP 플러그인 수동 설치
  • YS on Windows 10 iCloud 사진 저장 폴더 변경
  • 편리 on Docker를 이용한 Centos7 + httpd + php 5.4 개발환경 구축

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
© 2025 CHICPRO | Built using WordPress and SuperbThemes