cfmgrv4/app/Controllers/WebhookController.php
2025-02-13 16:45:08 +09:00

47 lines
1.3 KiB
PHP

<?php
namespace App\Controllers;
use CodeIgniter\Controller;
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 . 'webhook.txt';
// 데이터를 JSON 형식으로 변환하고 새 줄 문자 추가
$logEntry = json_encode($logData, JSON_PRETTY_PRINT) . "\n---\n";
// 파일에 데이터 추가
if (file_put_contents($filePath, $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'
]);
}
}