worker test
This commit is contained in:
68
app/Libraries/NaverApiClient.php
Normal file
68
app/Libraries/NaverApiClient.php
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Libraries;
|
||||||
|
|
||||||
|
class NaverApiClient
|
||||||
|
{
|
||||||
|
protected $baseUrl = 'https://test-b2b.land.naver.com';
|
||||||
|
protected $charger = 'admin';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [GET] 매물 정보 조회
|
||||||
|
*/
|
||||||
|
public function getArticleInfo(string $articleNumber): ?array
|
||||||
|
{
|
||||||
|
$url = "{$this->baseUrl}/kiso/center/verification-article/{$articleNumber}?charger={$this->charger}";
|
||||||
|
|
||||||
|
return $this->request('GET', $url);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [PUT] 매물 정보 수정
|
||||||
|
* @param string $articleNumber 매물번호
|
||||||
|
* @param array $updateData 수정할 데이터 (tradeType, price, space 등)
|
||||||
|
*/
|
||||||
|
public function updateArticleInfo(string $articleNumber, array $updateData): ?array
|
||||||
|
{
|
||||||
|
$url = "{$this->baseUrl}/kiso/center/verification-article/{$articleNumber}?charger={$this->charger}";
|
||||||
|
|
||||||
|
return $this->request('PUT', $url, $updateData);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* CURL 공통 실행 함수
|
||||||
|
*/
|
||||||
|
private function request(string $method, string $url, array $data = null): ?array
|
||||||
|
{
|
||||||
|
$ch = curl_init();
|
||||||
|
curl_setopt($ch, CURLOPT_URL, $url);
|
||||||
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||||
|
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
|
||||||
|
|
||||||
|
if ($method === 'PUT') {
|
||||||
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
|
||||||
|
if ($data) {
|
||||||
|
$payload = json_encode($data);
|
||||||
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
|
||||||
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
||||||
|
'Content-Type: application/json',
|
||||||
|
'Content-Length: ' . strlen($payload)
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$response = curl_exec($ch);
|
||||||
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||||
|
curl_close($ch);
|
||||||
|
|
||||||
|
// 결과 로그 기록 (성공/실패 모두 기록하여 추적 가능하게 함)
|
||||||
|
if ($httpCode === 200) {
|
||||||
|
log_message('info', "[Naver API $method SUCCESS] URL: $url | Response: $response");
|
||||||
|
} else {
|
||||||
|
log_message('error', "[Naver API $method FAIL] URL: $url | Code: $httpCode | Response: $response");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return json_decode($response, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,20 +4,26 @@
|
|||||||
* - 프레임워크를 로드하지 않아 매우 빠르고 안전함
|
* - 프레임워크를 로드하지 않아 매우 빠르고 안전함
|
||||||
* - 받은 데이터를 Redis 큐에 넣고 즉시 응답
|
* - 받은 데이터를 Redis 큐에 넣고 즉시 응답
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// 1. 응답 헤더 설정 (JSON)
|
// 1. 응답 헤더 설정 (JSON)
|
||||||
header('Content-Type: application/json; charset=utf-8');
|
header('Content-Type: application/json; charset=utf-8');
|
||||||
|
|
||||||
// 2. 보안 키 체크 (URL 파라미터 key=값)
|
// 2. 보안 키 체크 (URL 파라미터 key=값)
|
||||||
$configKey = "YOUR_SECRET_KEY_HERE"; // 실제 사용할 키값으로 변경하세요
|
$configKey = "7EE868F4B36D36B3D86736828F4729EAC4992083"; // 실제 사용할 키값으로 변경하세요
|
||||||
|
$api_info = [
|
||||||
|
'@type' => 'response',
|
||||||
|
'@service' => 'confirms',
|
||||||
|
'@version' => '1.0.0'
|
||||||
|
];
|
||||||
$receivedKey = $_GET['key'] ?? '';
|
$receivedKey = $_GET['key'] ?? '';
|
||||||
|
$logDir = __DIR__ . '/logs/';
|
||||||
|
|
||||||
if ($receivedKey !== $configKey) {
|
if ($receivedKey !== $configKey) {
|
||||||
http_response_code(403);
|
http_response_code(403);
|
||||||
echo json_encode([
|
$api_info['error'] = [
|
||||||
'resultCode' => 'E003',
|
'code' => '-1',
|
||||||
'resultMessage' => 'Invalid API Key'
|
'message' => 'Unregistered key'
|
||||||
]);
|
];
|
||||||
|
echo json_encode($api_info);
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,11 +32,6 @@ try {
|
|||||||
$rawData = file_get_contents('php://input');
|
$rawData = file_get_contents('php://input');
|
||||||
$data = json_decode($rawData, true);
|
$data = json_decode($rawData, true);
|
||||||
|
|
||||||
// JSON 데이터가 비어있다면 GET 파라미터 사용 (테스트용)
|
|
||||||
if (empty($data)) {
|
|
||||||
$data = $_GET;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (empty($data)) {
|
if (empty($data)) {
|
||||||
throw new Exception("Empty data received");
|
throw new Exception("Empty data received");
|
||||||
}
|
}
|
||||||
@@ -44,7 +45,7 @@ try {
|
|||||||
throw new Exception("Could not connect to Redis");
|
throw new Exception("Could not connect to Redis");
|
||||||
}
|
}
|
||||||
|
|
||||||
$redis->select(10); // 10번 DB 사용
|
$redis->select(9); // 10번 DB 사용
|
||||||
|
|
||||||
// 5. 큐에 넣을 데이터 포맷팅
|
// 5. 큐에 넣을 데이터 포맷팅
|
||||||
$payload = [
|
$payload = [
|
||||||
@@ -56,6 +57,11 @@ try {
|
|||||||
// 'naver:raw_queue'라는 이름의 리스트에 저장
|
// 'naver:raw_queue'라는 이름의 리스트에 저장
|
||||||
$redis->lPush('naver:raw_queue', json_encode($payload));
|
$redis->lPush('naver:raw_queue', json_encode($payload));
|
||||||
|
|
||||||
|
// --- [여기서부터 로그 저장 코드 추가] ---
|
||||||
|
// 들어온 원본($rawData)을 그대로 기록합니다.
|
||||||
|
writeLog("RAW_RECEIVE | " . $rawData, 'INFO');
|
||||||
|
// --------------------------------------
|
||||||
|
|
||||||
// 6. 네이버측에 성공 응답 (202 Accepted)
|
// 6. 네이버측에 성공 응답 (202 Accepted)
|
||||||
// 처리가 완료된 것은 아니지만, 접수는 완료되었음을 의미
|
// 처리가 완료된 것은 아니지만, 접수는 완료되었음을 의미
|
||||||
http_response_code(202);
|
http_response_code(202);
|
||||||
@@ -67,11 +73,39 @@ try {
|
|||||||
|
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
// 7. 장애 발생 시 로그 기록 (시스템 로그)
|
// 7. 장애 발생 시 로그 기록 (시스템 로그)
|
||||||
error_log("[API_RECEIVER_ERROR] " . $e->getMessage());
|
|
||||||
|
$api_info['error'] = [
|
||||||
|
'code' => '-1',
|
||||||
|
'message' => $e->getMessage()
|
||||||
|
];
|
||||||
|
writeLog( json_encode($api_info) ." | Received: " . json_encode($data), 'ERROR');
|
||||||
|
|
||||||
http_response_code(500);
|
http_response_code(500);
|
||||||
echo json_encode([
|
|
||||||
'resultCode' => 'E999',
|
echo json_encode($api_info);
|
||||||
'resultMessage' => 'Internal System Error: ' . $e->getMessage()
|
}
|
||||||
]);
|
|
||||||
|
/**
|
||||||
|
* 날짜별 로그 기록 함수
|
||||||
|
* @param string $message 로그 내용
|
||||||
|
* @param string $level 로그 레벨 (INFO, ERROR, DEBUG 등)
|
||||||
|
*/
|
||||||
|
function writeLog($message, $level = 'ERROR') {
|
||||||
|
// 1. 로그 저장 경로 설정 (프로젝트 루트의 logs 폴더)
|
||||||
|
$logBaseDir = __DIR__ . '/logs';
|
||||||
|
$Dir = $logBaseDir . '/';
|
||||||
|
|
||||||
|
// 2. 폴더가 없으면 생성 (연/월 구조로 관리하면 파일이 너무 많아지는 것을 방지)
|
||||||
|
if (!is_dir($Dir)) {
|
||||||
|
mkdir($Dir, 0777, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. 파일명 결정 (예: logs/2025/12/2025-12-22.log)
|
||||||
|
$logFile = $Dir . '/' . date('Y-m-d') . '.log';
|
||||||
|
// 4. 로그 포맷팅 (시간 [레벨] 메시지)
|
||||||
|
$timestamp = date('Y-m-d H:i:s');
|
||||||
|
$formattedMessage = "[$timestamp] [$level] $message" . PHP_EOL;
|
||||||
|
|
||||||
|
// 5. 파일 기록 (FILE_APPEND로 기존 내용 뒤에 추가)
|
||||||
|
file_put_contents($logFile, $formattedMessage, FILE_APPEND);
|
||||||
}
|
}
|
||||||
2
worker/logs/2025-12-22.log
Normal file
2
worker/logs/2025-12-22.log
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
[2025-12-22 12:54:37] [ERROR] {"@type":"response","@service":"confirms","@version":"1.0.0","error":{"code":"-1","message":"Empty data received"}} | Received: null
|
||||||
|
[2025-12-22 13:03:56] [ERROR] {"@type":"response","@service":"confirms","@version":"1.0.0","error":{"code":"-1","message":"Empty data received"}} | Received: null
|
||||||
Reference in New Issue
Block a user