PHP
Installation
Section titled “Installation”No additional packages required — PHP includes cURL by default.
Basic Example
Section titled “Basic Example”<?phpfunction verifySubmission(array $formData): array { $ch = curl_init('https://api.formsentry.ai/v1/verify');
curl_setopt_array($ch, [ CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true, CURLOPT_TIMEOUT => 5, CURLOPT_HTTPHEADER => ['Content-Type: application/json'], CURLOPT_POSTFIELDS => json_encode([ 'apiKey' => getenv('FORMSENTRY_API_KEY'), 'formId' => getenv('FORMSENTRY_FORM_ID'), 'payload' => $formData ]) ]);
$response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch);
if ($httpCode !== 200) { throw new Exception("FormSentry error: $httpCode"); }
return json_decode($response, true);}
// Usage$result = verifySubmission([ 'name' => 'John Doe', 'email' => 'john@example.com', 'message' => 'I would like to learn more about your services.']);
if ($result['status'] === 'spam') { echo "Spam detected: " . $result['reasoning'];} else { echo "Legitimate submission";}?>Laravel Integration
Section titled “Laravel Integration”<?phpnamespace App\Http\Controllers;
use Illuminate\Http\Request;use Illuminate\Support\Facades\Http;use Illuminate\Support\Facades\Log;
class ContactController extends Controller{ public function store(Request $request) { $validated = $request->validate([ 'name' => 'required|string|max:255', 'email' => 'required|email', 'message' => 'required|string', ]);
try { $verification = Http::timeout(5)->post('https://api.formsentry.ai/v1/verify', [ 'apiKey' => config('services.formsentry.api_key'), 'formId' => config('services.formsentry.form_id'), 'payload' => $validated ]);
$verification->throw(); $result = $verification->json(); } catch (\Exception $e) { // Fail open — if FormSentry is unreachable, allow the submission Log::error('FormSentry error: ' . $e->getMessage()); $result = ['status' => 'legitimate', 'confidence' => 0.0]; }
if (($result['status'] ?? null) === 'spam') { // Silently accept to avoid tipping off spammers return response()->json(['success' => true]); }
// TODO: Process the legitimate submission // e.g., Mail::to(...)->send(new ContactSubmission($validated));
return response()->json(['success' => true]); }}Add your credentials to config/services.php:
'formsentry' => [ 'api_key' => env('FORMSENTRY_API_KEY'), 'form_id' => env('FORMSENTRY_FORM_ID'),],Vanilla PHP Form Handler
Section titled “Vanilla PHP Form Handler”<?phpfunction verifySubmission(array $formData): array { $ch = curl_init('https://api.formsentry.ai/v1/verify');
curl_setopt_array($ch, [ CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true, CURLOPT_TIMEOUT => 5, CURLOPT_HTTPHEADER => ['Content-Type: application/json'], CURLOPT_POSTFIELDS => json_encode([ 'apiKey' => getenv('FORMSENTRY_API_KEY'), 'formId' => getenv('FORMSENTRY_FORM_ID'), 'payload' => $formData ]) ]);
$response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch);
if ($httpCode !== 200) { throw new Exception("FormSentry error: $httpCode"); }
return json_decode($response, true);}
if ($_SERVER['REQUEST_METHOD'] === 'POST') { $formData = [ 'name' => trim($_POST['name'] ?? ''), 'email' => trim($_POST['email'] ?? ''), 'message' => trim($_POST['message'] ?? ''), ];
// Validate input if (empty($formData['name']) || empty($formData['email']) || empty($formData['message'])) { http_response_code(400); echo "Please fill in all fields."; exit; }
try { $result = verifySubmission($formData); } catch (Exception $e) { // Fail open — if FormSentry is unreachable, allow the submission error_log('FormSentry error: ' . $e->getMessage()); $result = ['status' => 'legitimate', 'confidence' => 0.0]; }
if (($result['status'] ?? null) === 'spam') { // Show success message anyway (don't reveal detection) echo "Thank you for your message!"; exit; }
// TODO: Process the legitimate submission mail('you@example.com', 'Contact Form', $formData['message']); echo "Thank you for your message!";}?>Error Handling
Section titled “Error Handling”<?phpfunction verifyWithRetry(array $payload, int $maxRetries = 3): array { for ($attempt = 1; $attempt <= $maxRetries; $attempt++) { $ch = curl_init('https://api.formsentry.ai/v1/verify');
curl_setopt_array($ch, [ CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true, CURLOPT_TIMEOUT => 5, CURLOPT_HTTPHEADER => ['Content-Type: application/json'], CURLOPT_POSTFIELDS => json_encode([ 'apiKey' => getenv('FORMSENTRY_API_KEY'), 'formId' => getenv('FORMSENTRY_FORM_ID'), 'payload' => $payload ]) ]);
$response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch);
if ($httpCode === 200) { return json_decode($response, true); }
if (in_array($httpCode, [429, 500, 502, 503])) { $delay = min(pow(2, $attempt - 1), 5); sleep($delay); continue; }
throw new Exception("FormSentry error: $httpCode"); }
throw new Exception('Max retries exceeded');}?>See Errors for all error codes and retry strategies.