cfmgrv4/app/Controllers/Utils/WebhookController.php

53 lines
1.5 KiB
PHP

<?php
namespace App\Controllers\Utils;
use App\Controllers\CommonController;
class WebhookController extends CommonController
{
public function cloudflare()
{
// POST 데이터 받기
$jsonData = $this->request->getJSON();
if (empty($jsonData)) {
return $this->response->setStatusCode(400)->setJSON([
'status' => 'error',
'message' => 'Invalid JSON data'
]);
}
// 현재 시간 추가
$logData = [
'timestamp' => date('Y-m-d H:i:s'),
'data' => $jsonData
];
// 파일 경로 설정
$filePath = WRITEPATH . DIRECTORY_SEPARATOR . "webhook";
//디렉토리 생성 여부 확인
if (!is_dir($filePath)) {
mkdir($filePath, 0755, true);
}
$fullpath = $filePath . DIRECTORY_SEPARATOR . date('Y-m-d') . '.log';
// 데이터를 JSON 형식으로 변환하고 새 줄 문자 추가
$logEntry = json_encode($logData, JSON_PRETTY_PRINT) . "\n---\n";
// 파일에 데이터 추가
if (file_put_contents($fullpath, $logEntry, FILE_APPEND) === false) {
return $this->response->setStatusCode(500)->setJSON([
'status' => 'error',
'message' => 'Failed to write to file'
]);
}
return $this->response->setJSON([
'status' => 'success',
'message' => 'Webhook data logged successfully'
]);
}
}