PHP 연동

SilentShield로 PHP 폼을 보호하세요. 이 가이드에서는 Laravel, Symfony 및 네이티브 PHP를 다룹니다.

1. 프론트엔드 설정

템플릿(Blade, Twig 또는 일반 HTML)에 SilentShield 스크립트를 추가합니다:

HTML + PHPphp
<script src="https://api.silentshield.io/client.js?key=<?= $apiKey ?>" defer></script>
<script>
  document.addEventListener('DOMContentLoaded', function() {
    SilentShield.init({ apiKey: '<?= $apiKey ?>' });
  });
</script>

2a. 네이티브 PHP 검증

폼 제출 시 Nonce를 검증합니다:

PHPphp
<?php
$nonce = $_POST['ss_nonce'] ?? '';
if (empty($nonce)) {
    http_response_code(422);
    die('Missing verification nonce');
}

$ch = curl_init('https://api.silentshield.io/api/v1/captcha/verify-nonce');
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_HTTPHEADER => [
        'Content-Type: application/json',
        'X-Api-Key: ' . SILENTSHIELD_API_KEY,
    ],
    CURLOPT_POSTFIELDS => json_encode(['nonce' => $nonce]),
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT => 5,
]);
$result = json_decode(curl_exec($ch), true);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if ($httpCode !== 200 || ($result['verdict'] ?? '') === 'bot') {
    http_response_code(403);
    die('Bot detected');
}

// Form is safe — continue processing
?>

2b. Laravel 미들웨어

모든 POST 요청에서 자동으로 Nonce를 검증하는 미들웨어를 생성합니다:

Laravel Middlewarephp
<?php
// app/Http/Middleware/VerifySilentShield.php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class VerifySilentShield
{
    public function handle(Request $request, Closure $next)
    {
        $nonce = $request->input('ss_nonce');
        if (!$nonce) {
            return response()->json(['error' => 'Missing nonce'], 422);
        }

        $response = Http::withHeaders([
            'X-Api-Key' => config('services.silentshield.key'),
        ])->timeout(5)->post(
            'https://api.silentshield.io/api/v1/captcha/verify-nonce',
            ['nonce' => $nonce]
        );

        if ($response->json('verdict') === 'bot') {
            return response()->json(['error' => 'Bot detected'], 403);
        }

        return $next($request);
    }
}

미들웨어 등록

bootstrap/app.php 또는 Kernel.php에서 라우트에 미들웨어를 추가합니다:

routes/web.phpphp
// routes/web.php
Route::post('/contact', [ContactController::class, 'submit'])
    ->middleware(VerifySilentShield::class);

WordPress

WordPress 연동에 대해서는 전용 WordPress 가이드를 참조하세요.