Sign In

PHP 샘플 코드

임시로 엑셀에서 만들 채널 리스트를 가져오게 했습니다.

1.
composer.json 파일에 의존성 추가:
엑셀 파일을 읽기 위해 PhpSpreadsheet 라이브러리를 설치해야 합니다.
composer require phpoffice/phpspreadsheet

채널 생성 코드

엑셀 파일에 채널 정보를 읽어와서 채널을 전부 생성하는 샘플입니다.
<?php

require 'vendor/autoload.php';  // PhpSpreadsheet 로드

use PhpOffice\PhpSpreadsheet\IOFactory;

function auto_create_channels() {
    $file_path = '전북RIS_강좌 정보.xlsx';
    $login_api_url = "https://usamsung-api.veluga.app/user/sign-in";
    $create_channel_api_url = "https://usamsung-api.veluga.app/channel/";

    $token_cache = []; // 이메일별 토큰 캐시

    // 엑셀 파일 불러오기
    $spreadsheet = IOFactory::load($file_path);
    $worksheet = $spreadsheet->getSheetByName('과목 채널 생성용');
    $rows = $worksheet->toArray();

    // 첫 번째 행은 헤더이므로 생략
    array_shift($rows);

    foreach ($rows as $row) {
        try {
            $target_email = trim($row[4]);

            // 이메일이 캐시에 있는지 확인
            if (!isset($token_cache[$target_email])) {
                // 로그인 요청
                $login_body = json_encode([
                    "email" => $target_email,
                    "password" => $target_email,
                    "isBusiness" => true
                ]);

                $login_response = api_request('POST', $login_api_url, $login_body);
                if ($login_response['http_code'] != 200) {
                    echo "로그인 실패: " . $login_response['response'] . "\n";
                    continue;
                }

                $token_cache[$target_email] = json_decode($login_response['response'], true)['data']['token'];
            }

            $target_token = $token_cache[$target_email];
            $headers = [
                "Authorization: Bearer $target_token",
                "Content-Type: application/json"
            ];

            // 채널 목록 가져오기
            $channel_list_response = api_request('GET', $create_channel_api_url, '', $headers);
            $channel_list = json_decode($channel_list_response['response'], true)['data']['list'];
            $channel_name_list = array_column($channel_list, 'name');

            $sheet_channel_name = $row[6];
            $channel_description = $row[7];

            if (!in_array($sheet_channel_name, $channel_name_list)) {
                // 채널 생성
                $create_channel_body = json_encode([
                    "name" => $sheet_channel_name,
                    "description" => $channel_description,
                    "welcomeMessage" => "안녕하세요. 과목을 수강하면서 궁금한 사항을 무엇이든 말씀해주시면 친절히 알려드릴게요.\n저는 강의교안과 교수님의 영상 자막을 기반으로 학습되었습니다.",
                    "temperature" => 'half',
                    "answerTypeIfCant" => ['noAnswer'],
                    "answerType" => 'respectful, long and detailed',
                    "isPublic" => 'false',
                    "enableQuestionHint" => 'true',
                    "enableFollowQuestion" => 'false',
                    "shouldReturnFile" => 'false',
                    "searchModel" => "hybrid",
                    "commonGpt" => "gpt-4o"
                ]);

                $response = api_request('POST', $create_channel_api_url, $create_channel_body, $headers);
                echo $response['response'] . "\n";
            } else {
                // 채널 업데이트
                $channel_id = array_search($sheet_channel_name, array_column($channel_list, 'name'));
                $update_channel_url = $create_channel_api_url . $channel_list[$channel_id]['_id'];

                $update_channel_body = json_encode([
                    "description" => $channel_description,
                    "welcomeMessage" => "안녕하세요. 과목을 수강하면서 궁금한 사항을 무엇이든 말씀해주시면 친절히 알려드릴게요.\n저는 강의교안과 교수님의 영상 자막을 기반으로 학습되었습니다.",
                    "commonGpt" => "gpt-4o"
                ]);

                $response = api_request('PATCH', $update_channel_url, $update_channel_body, $headers);
                echo $response['response'] . "\n";
            }
        } catch (Exception $e) {
            echo "에러 발생: " . $e->getMessage() . "\n";
        }
    }
}

function api_request($method, $url, $body = '', $headers = []) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);

    if (!empty($body)) {
        curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
    }

    if (!empty($headers)) {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    }

    $response = curl_exec($ch);
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    return [
        'response' => $response,
        'http_code' => $http_code
    ];
}

// 실행
auto_create_channels();

?>

공지사항(게시물/게시판 학습) API PHP

<?php

function train_post_and_check_status($api_key, $channel_id, $title, $content, $board = null, $from_url = null, $reg_datetime = null, $metadata = null, $doc_filter = null, $attachments = null) {
    // API URL 설정
    $post_url = "https://usamsung-api.veluga.app/api/v1/document/post";
    $check_url_template = "https://usamsung-api.veluga.app/api/v1/document/post/{id}";

    // 헤더 설정
    $headers = [
        "X-VELUGA-API-KEY: $api_key",
        "Content-Type: application/json"
    ];

    // 요청 데이터 설정
    $data = [
        "channelId" => $channel_id,
        "title" => $title,
        "content" => $content,
        "board" => $board,
        "fromUrl" => $from_url,
        "regDatetime" => $reg_datetime,
        "metadata" => $metadata,
        "attachments" => $attachments,
        "docFilter" => $doc_filter ? json_encode($doc_filter) : null
    ];

    // null 값을 제거한 후 JSON으로 인코딩
    $data = array_filter($data, fn($value) => $value !== null);
    $json_data = json_encode($data);

    // 게시물 학습 요청
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $post_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    $response = curl_exec($ch);
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    if ($http_code == 200) {
        $response_data = json_decode($response, true);
        $post_id = $response_data['data'];
        echo "게시물 학습 요청 성공. 게시물 ID: $post_id\n";

        // 학습 상태 체크 (2초 간격)
        while (true) {
            $check_url = str_replace("{id}", $post_id, $check_url_template);
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $check_url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            
            $check_response = curl_exec($ch);
            $check_http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            curl_close($ch);

            if ($check_http_code == 200) {
                $check_data = json_decode($check_response, true)['data'];
                $post_state = $check_data['state'];
                
                echo "게시물 상태: $post_state\n";
                
                if (in_array($post_state, ['success', 'fail'])) {
                    echo "학습이 $post_state 상태로 완료되었습니다.\n";
                    break;
                }
            } else {
                echo "학습 상태 체크 실패. 응답 코드: $check_http_code\n";
                break;
            }

            sleep(2); // 2초 대기
        }
    } else {
        echo "게시물 학습 요청 실패. 응답 코드: $http_code\n";
        echo "응답 내용: $response\n";
    }
}

// 예시 사용법
$api_key = 'your_api_key_here';
$channel_id = 'your_channel_id_here';
$title = '게시물 제목';
$content = '<p>게시물 내용입니다.</p>';
$board = '공지사항 게시판';
$from_url = 'https://example.com/post';
$reg_datetime = '2024-08-13T12:00:00Z';
$metadata = [
    'author' => '관리자'
];
$doc_filter = [
    'docType' => '공지사항'
];
$attachments = [
    [
        'name' => 'attachment.pdf',
        'fileUrl' => 'https://example.com/attachment.pdf'
    ]
];

train_post_and_check_status($api_key, $channel_id, $title, $content, $board, $from_url, $reg_datetime, $metadata, $doc_filter, $attachments);

?>

공지사항 게시물 학습 상태 조회

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://usamsung-api.veluga.app/api/v1/document/post/66c46...[채널아이디]',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'GET',
  CURLOPT_HTTPHEADER => array(
    'X-VELUGA-API-KEY: your_api_key_here ',
    'Content-Type: application/json'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

공지사항 (게시물) 삭제

*주의:학습중인 상태 (파일/게시판/weburl )에서 delete 하면 안됩니다

<?php

function get_failed_posts($api_key, $channel_id, $board_name = null, $keyword = null) {
    // 게시물 리스트 조회 API URL 설정
    $list_url = "https://usamsung-api.veluga.app/api/v1/document/post";

    // 요청 헤더 설정
    $headers = [
        "X-VELUGA-API-KEY: $api_key",
        "Content-Type: application/json"
    ];

    // 요청 파라미터 설정
    $params = [
        "channelId" => $channel_id,
        "boardName" => $board_name,
        "keyword" => $keyword,
        "page" => 1,
        "limit" => 100, // 최대 100개의 게시물 조회
        "sortType" => "createdAt",
        "sort" => "desc"
    ];

    // 파라미터를 쿼리 스트링으로 변환
    $query = http_build_query($params);

    // cURL 초기화
    $ch = curl_init();

    // cURL 옵션 설정
    curl_setopt($ch, CURLOPT_URL, "$list_url?$query");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    // 요청 실행 및 응답 받기
    $response = curl_exec($ch);
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    // cURL 종료
    curl_close($ch);

    // 응답 코드가 200이면 데이터 처리
    if ($http_code == 200) {
        $response_data = json_decode($response, true);
        $failed_posts = array_filter($response_data['data']['list'], function($post) {
            return $post['state'] == 'fail';
        });
        return $failed_posts;
    } else {
        echo "게시물 리스트 조회 실패. 응답 코드: $http_code\n";
        echo "응답 내용: $response\n";
        return null;
    }
}

function delete_failed_post($api_key, $post_id) {
    // 게시물 삭제 API URL 설정
    $delete_url = "https://usamsung-api.veluga.app/api/v1/document/$post_id";

    // 요청 헤더 설정
    $headers = [
        "X-VELUGA-API-KEY: $api_key"
    ];

    // cURL 초기화
    $ch = curl_init();

    // cURL 옵션 설정
    curl_setopt($ch, CURLOPT_URL, $delete_url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    // 요청 실행 및 응답 받기
    $response = curl_exec($ch);
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    // cURL 종료
    curl_close($ch);

    // 응답 코드가 200이면 삭제 성공 메시지 출력
    if ($http_code == 200) {
        echo "게시물 삭제 성공. 게시물 ID: $post_id\n";
    } else {
        echo "게시물 삭제 실패. 게시물 ID: $post_id, 응답 코드: $http_code\n";
        echo "응답 내용: $response\n";
    }
}

function delete_all_failed_posts($api_key, $channel_id, $board_name = null, $keyword = null) {
    // 학습이 실패한 게시물 리스트 가져오기
    $failed_posts = get_failed_posts($api_key, $channel_id, $board_name, $keyword);

    if (!empty($failed_posts)) {
        echo "총 " . count($failed_posts) . "개의 학습 실패 게시물이 발견되었습니다.\n";
        foreach ($failed_posts as $post) {
            delete_failed_post($api_key, $post['_id']);
        }
    } else {
        echo "학습 실패한 게시물이 없습니다.\n";
    }
}

?>

파일 학습 예시 (중복파일 강제업데이트 옵션)

<?php

function train_file_and_check_status($api_key, $channel_id, $file_path, $doc_filter = null) {
    // API URL 설정
    $upload_url = "https://usamsung-api.veluga.app/api/v1/document/file";
    $check_url_template = "https://usamsung-api.veluga.app/api/v1/document/file/{id}";

    // 헤더 설정
    $headers = [
        "X-VELUGA-API-KEY: $api_key"
    ];

    // 데이터 설정
    $post_fields = [
        'channelId' => $channel_id,
        'forceUpdate' => 'true'
    ];

    if ($doc_filter !== null) {
        $post_fields['docFilter'] = json_encode($doc_filter); // doc_filter를 JSON 문자열로 변환
    }

    // 파일 추가
    $post_fields['file'] = curl_file_create($file_path);

    // 파일 업로드 요청
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $upload_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    
    $response = curl_exec($ch);
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    if ($http_code == 200) {
        $response_data = json_decode($response, true);
        $file_id = $response_data['data'];
        $success = $response_data['success'];

        if (!$success) {
            echo "파일 업로드 실패. 응답 코드: $http_code\n";
            echo "응답 내용: $response\n";
            return;
        }
        echo "파일 업로드 성공. 파일 ID: $file_id\n";

        // 학습 상태 체크 (2초 간격)
        while (true) {
            $check_url = str_replace("{id}", $file_id, $check_url_template);
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $check_url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            
            $check_response = curl_exec($ch);
            $check_http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            curl_close($ch);

            if ($check_http_code == 200) {
                $check_data = json_decode($check_response, true)['data'];
                $file_state = $check_data['state'];
                
                echo "파일 상태: $file_state\n";
                
                if ($file_state === 'success' || $file_state === 'fail') {
                    echo "학습이 $file_state 상태로 완료되었습니다.\n";
                    break;
                }
            } else {
                echo "학습 상태 체크 실패. 응답 코드: $check_http_code\n";
                break;
            }

            sleep(2); // 2초 대기
        }
    } else {
        echo "파일 업로드 실패. 응답 코드: $http_code\n";
        echo "응답 내용: $response\n";
    }
}

// 예시 사용법
$api_key = 'your_api_key_here';
$channel_id = 'your_channel_id_here';
$file_path = '/path/to/your/file.pdf';  // 실제 파일 경로로 변경하세요
$doc_filter = [
    'docType' => '공지사항'
];

train_file_and_check_status($api_key, $channel_id, $file_path, $doc_filter);

?>

파일 삭제 예시

<?php

function delete_files($api_key, $file_id_list) {
    // API URL 설정
    $delete_url = "https://usamsung-api.veluga.app/api/v1/document/file";

    // 요청 헤더 설정
    $headers = [
        "Authorization: Bearer $api_key",
        "Content-Type: application/json"
    ];

    // 요청 데이터 설정
    $data = [
        "fileIdList" => $file_id_list
    ];

    // JSON 형식으로 데이터 인코딩
    $json_data = json_encode($data);

    // cURL 초기화
    $ch = curl_init();

    // cURL 옵션 설정
    curl_setopt($ch, CURLOPT_URL, $delete_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    // 요청 실행 및 응답 받기
    $response = curl_exec($ch);
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    // cURL 종료
    curl_close($ch);

    // 응답 처리
    if ($http_code == 200) {
        $response_data = json_decode($response, true);
        echo "파일 삭제 요청 성공. 응답:\n";
        print_r($response_data);
    } else {
        echo "파일 삭제 요청 실패. 응답 코드: $http_code\n";
        echo "응답 내용: $response\n";
    }
}

// 예시 사용법
$api_key = 'your_api_key_here';  // 실제 API 키를 입력하세요
$file_id_list = ["60f9b3b3e3b9f3a7b4b3b3b3", "60f9b3b3e3b9f3a7b4b3b3b4"];  // 삭제할 파일 ID 리스트

delete_files($api_key, $file_id_list);

?>

WebUrl 학습 예제

<?php

function train_webUrl_and_check_status($api_key, $channel_id, $web_url, $doc_filter = null) {
    // API URL 설정
    $upload_url = "https://usamsung-api.veluga.app/api/v1/document/web-url";
    $check_url_template = "https://usamsung-api.veluga.app/api/v1/document/web-url/{id}";

    // 헤더 설정
    $headers = [
        "X-VELUGA-API-KEY: $api_key"
    ];

    // 데이터 설정
    $post_fields = [
        'channelId' => $channel_id,
        'webUrl' => $web_url,
        'forceUpdate' => 'true'
    ];

    if ($doc_filter !== null) {
        $post_fields['docFilter'] = json_encode($doc_filter); // doc_filter를 JSON 문자열로 변환
    }

    // 웹 URL 업로드 요청
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $upload_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    
    $response = curl_exec($ch);
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    if ($http_code == 200) {
        $response_data = json_decode($response, true);
        $web_id = $response_data['data'];
        $success = $response_data['success'];

        if ($success === false) {
            echo "웹 URL 업로드 실패. 응답 코드: $http_code\n";
            echo "응답 내용: $response\n";
            return;
        }

        echo "웹 업로드 성공. 파일 ID: $web_id\n";

        // 학습 상태 체크 (2초 간격)
        while (true) {
            $check_url = str_replace("{id}", $web_id, $check_url_template);
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $check_url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            
            $check_response = curl_exec($ch);
            $check_http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            curl_close($ch);

            if ($check_http_code == 200) {
                $check_data = json_decode($check_response, true)['data'];
                $file_state = $check_data['state'];
                
                echo "웹 URL 상태: $file_state\n";
                
                if ($file_state === 'success' || $file_state === 'fail') {
                    echo "학습이 $file_state 상태로 완료되었습니다.\n";
                    break;
                }
            } else {
                echo "학습 상태 체크 실패. 응답 코드: $check_http_code\n";
                break;
            }

            sleep(2); // 2초 대기
        }
    } else {
        echo "웹 URL 업로드 실패. 응답 코드: $http_code\n";
        echo "응답 내용: $response\n";
    }
}

// 예시 사용법
$api_key = 'your_api_key_here';
$channel_id = 'your_channel_id_here';
$web_url = 'https://example.com/your-web-url';
$doc_filter = [
    'docType' => '공지사항'
];

train_webUrl_and_check_status($api_key, $channel_id, $web_url, $doc_filter);

?>

참조 파일 학습 (csv 먼저 학습후 pdf 참조 링크)

<?php

function train_csv_and_check_status($api_key, $channel_id, $file_path) {
    // CSV 파일 학습 및 상태 확인 URL 설정
    $upload_url = "https://usamsung-api.veluga.app/api/v1/document/file";
    $check_url_template = "https://usamsung-api.veluga.app/api/v1/document/file/{id}";

    // CSV 파일 여부 확인
    if (pathinfo($file_path, PATHINFO_EXTENSION) !== 'csv') {
        echo "Error: 이 함수는 CSV 파일만 업로드할 수 있습니다.\n";
        return [null, null];
    }

    // 헤더 설정
    $headers = [
        "X-VELUGA-API-KEY: $api_key"
    ];

    // 파일 및 데이터 설정
    $post_fields = [
        "file" => new CURLFile($file_path),
        "channelId" => $channel_id
    ];

    // cURL 초기화
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $upload_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    // CSV 파일 학습 요청
    $response = curl_exec($ch);
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    if ($http_code == 200) {
        $response_data = json_decode($response, true);
        $file_id = $response_data['data'];
        echo "CSV 파일 업로드 성공. 파일 ID: $file_id\n";

        // 학습 상태 체크 (2초 간격)
        while (true) {
            $check_url = str_replace("{id}", $file_id, $check_url_template);
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $check_url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

            $check_response = curl_exec($ch);
            $check_http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            curl_close($ch);

            if ($check_http_code == 200) {
                $check_data = json_decode($check_response, true)['data'];
                $file_state = $check_data['state'];
                
                echo "CSV 파일 상태: $file_state\n";
                
                if (in_array($file_state, ['success', 'fail'])) {
                    echo "학습이 $file_state 상태로 완료되었습니다.\n";
                    return [$file_state, $file_id];
                }
            } else {
                echo "학습 상태 체크 실패. 응답 코드: $check_http_code\n";
                break;
            }

            sleep(2); // 2초 대기
        }
    } else {
        echo "CSV 파일 업로드 실패. 응답 코드: $http_code\n";
        echo "응답 내용: $response\n";
        return [null, null];
    }
}

function train_citation_file($api_key, $channel_id, $citation_file_path, $training_file_id) {
    // 참조 파일 학습 요청 URL 설정
    $citation_url = "https://usamsung-api.veluga.app/api/v1/document/file/citation";

    // 헤더 설정
    $headers = [
        "X-VELUGA-API-KEY: $api_key"
    ];

    // 파일 및 데이터 설정
    $post_fields = [
        "file" => new CURLFile($citation_file_path),
        "channelId" => $channel_id,
        "trainingFileId" => $training_file_id
    ];

    // cURL 초기화
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $citation_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    // 참조 파일 학습 요청
    $response = curl_exec($ch);
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    // 응답 처리
    if ($http_code == 200) {
        echo "참조 파일 학습 요청 성공.\n";
    } else {
        echo "참조 파일 학습 요청 실패. 응답 코드: $http_code\n";
        echo "응답 내용: $response\n";
    }
}

// 예시 사용법
$api_key = 'your_api_key_here';            // 실제 API 키 입력
$channel_id = 'your_channel_id_here';      // 실제 채널 ID 입력
$csv_file_path = 'testcsv.csv';            // CSV 파일 경로
$citation_file_path = 'testpdf.pdf';       // 참조 파일 경로

// CSV 파일 학습 요청 및 상태 확인
list($state, $file_id) = train_csv_and_check_status($api_key, $channel_id, $csv_file_path);

// 학습 성공 시 참조 파일 학습 요청
if ($state === "success") {
    train_citation_file($api_key, $channel_id, $citation_file_path, $file_id);
}

?>