TIL
PHP 코드이그나이터로 로컬 json 파일에 데이터 쓰기
개발따라김양
2024. 11. 22. 08:47
1. 문제 상황
PHP 코드이그나이터를 사용 하는 중에 정보를 json 파일에도 저장해야 했음
2. 코드
1. JsonController.php
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class JsonController extends CI_Controller
{
public function updateJson()
{
// JSON 파일 경로 설정
$filePath = FCPATH . 'writable/json/access.json';
// 요청 데이터 가져오기 (CI3 방식)
$inputData = file_get_contents('php://input'); // 원시 JSON 데이터 읽기
$requestData = json_decode($inputData, true); // JSON 문자열 -> 배열
if ($requestData === null) {
// 잘못된 JSON 데이터
$this->output
->set_status_header(400)
->set_content_type('application/json')
->set_output(json_encode(['error' => 'Invalid JSON data']));
return;
}
// 기존 JSON 파일 읽기
if (file_exists($filePath)) {
$jsonData = json_decode(file_get_contents($filePath), true);
if ($jsonData === null) {
// 기존 파일이 JSON 형식이 아님
$this->output
->set_status_header(500)
->set_content_type('application/json')
->set_output(json_encode(['error' => 'Error parsing existing JSON file']));
return;
}
} else {
$jsonData = [];
}
// 새 데이터 추가
$jsonData[] = $requestData;
// JSON 파일에 쓰기
if (file_put_contents($filePath, json_encode($jsonData, JSON_PRETTY_PRINT))) {
$this->output
->set_status_header(200)
->set_content_type('application/json')
->set_output(json_encode(['message' => 'JSON updated successfully']));
} else {
// 파일 쓰기 실패
$this->output
->set_status_header(500)
->set_content_type('application/json')
->set_output(json_encode(['error' => 'Failed to write to file']));
}
}
}
2. 데이터 보내는 파일
const newJson = {
nickname : "Rive",
age : 7
}; //Json 파일에 넣을 데이터
fetch('/jsonController/updateJson', { // 서버 API 엔드포인트
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(newJson), // JavaScript 객체를 JSON 문자열로 변환
})
.then(res => {
if (!res.ok) {
throw new Error(`HTTP error! Status: ${res.status}`);
}
return res.json();
})
.then(data => console.log('Success:', data))
.catch(err => {
console.error('Error:', err);
});