'server_misconfigured', 'message' => 'Set DATA_CHECKS_API_KEY']); return true; } $uri = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH) ?: '/'; $method = $_SERVER['REQUEST_METHOD'] ?? 'GET'; if ($method !== 'POST') { http_response_code(405); return true; } $raw = file_get_contents('php://input') ?: ''; $input = json_decode($raw, true); if (! is_array($input)) { $input = []; } if ($uri === '/api/validate-email') { $email = isset($input['email']) && is_string($input['email']) ? trim($input['email']) : ''; if ($email === '') { http_response_code(400); header('Content-Type: application/json; charset=utf-8'); echo json_encode(['error' => 'bad_request', 'message' => 'email required']); return true; } datachecks_proxy_forward($base, $key, '/v1/email', ['email' => $email]); return true; } if ($uri === '/api/validate-phone') { $phone = isset($input['phone']) && is_string($input['phone']) ? trim($input['phone']) : ''; if ($phone === '') { http_response_code(400); header('Content-Type: application/json; charset=utf-8'); echo json_encode(['error' => 'bad_request', 'message' => 'phone required']); return true; } datachecks_proxy_forward($base, $key, '/v1/phone', ['phone' => $phone]); return true; } return false; /** * @param array $payload */ function datachecks_proxy_forward(string $base, string $key, string $path, array $payload): void { $url = $base.$path; $json = json_encode($payload, JSON_THROW_ON_ERROR); $ch = curl_init($url); if ($ch === false) { http_response_code(500); return; } curl_setopt_array($ch, [ CURLOPT_POST => true, CURLOPT_POSTFIELDS => $json, CURLOPT_HTTPHEADER => [ 'Authorization: Bearer '.$key, 'Content-Type: application/json', 'Accept: application/json', ], CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 30, ]); $response = curl_exec($ch); $status = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); http_response_code($status > 0 ? $status : 502); if (! is_string($response)) { return; } if ($status === 200) { $decoded = json_decode($response, true); if (is_array($decoded)) { unset($decoded['remaining_credits']); header('Content-Type: application/json; charset=utf-8'); echo json_encode($decoded, JSON_UNESCAPED_UNICODE); return; } } $ct = 'application/json'; if ($status === 503 && $response === '') { $ct = 'text/plain'; } header('Content-Type: '.$ct.'; charset=utf-8'); echo $response; }