반응형
1. OpenAI API 키를 준비한다.
2. PHP로 OpenAI API로 연동한다.
<?php
// OpenAI API 키
$api_key = 'YOUR_OPENAI_API_KEY';
// 번역할 텍스트와 대상 언어 설정
$source_text = 'Hello, world!';
$target_language = 'Korean'; // 대상 언어 설정
// OpenAI API에 보낼 데이터 구성
$data = [
'prompt' => "Translate this into $target_language: $source_text",
'temperature' => 0.7,
'max_tokens' => 60,
'top_p' => 1,
'frequency_penalty' => 0,
'presence_penalty' => 0,
];
// cURL 세션 초기화
$ch = curl_init('https://api.openai.com/v1/engines/davinci/completions');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
"Authorization: Bearer $api_key"
]);
// 요청 실행 및 응답 받기
$response = curl_exec($ch);
curl_close($ch);
// 응답 처리
$response_data = json_decode($response, true);
$translated_text = $response_data['choices'][0]['text'];
echo "Original: $source_text\n";
echo "Translated: $translated_text\n";
?>
반응형
'DEVEL > PHP' 카테고리의 다른 글
라라벨 oAuth 서버 구축 (0) | 2024.03.10 |
---|---|
PHP POST 전송 (0) | 2024.02.22 |
PHP 내 지갑 트랜잭션 확인 (0) | 2024.01.19 |
PHP 이더리움 주소 검증 (0) | 2024.01.19 |
PHP 비트코인 주소 검증 (0) | 2024.01.19 |