PHP

PHP Integration

Secure your Laravel, Symfony, or WordPress sites with real-time content analysis.

Prerequisites

1. Installation

Start by installing the SafeComms SDK into your project using Composer.

$ composer require safecomms/safecomms-php

2. Initialize Client

Next, initialize the SafeComms client with your API key. This creates a connection to our analysis engine.

index.php
require 'vendor/autoload.php';

use SafeComms\SafeCommsClient;

// Initialize with your API Key
$client = new SafeCommsClient(getenv('SAFECOMMS_API_KEY'));

3. Add Logic

Add the moderation logic to your controller. This will scan the incoming request content before processing it further.

index.php
// In your controller or route handler
$content = $_POST['content'];

try {
    // Check content
    $result = $client->moderateText(
        content: $content,
        // pii: true // Enable PII detection (Starter Tier+)
    );

    if (!$result['isClean']) {
        http_response_code(400);
        echo json_encode($result);
        exit;
    }

    // Content is safe
    echo json_encode(['success' => true]);

} catch (Exception $e) {
    http_response_code(500);
    echo json_encode(['error' => 'Moderation check failed']);
}

4. Verify & Test

Finally, verify your integration is working correctly by sending a test request.

Terminal
curl -X POST http://localhost:8000/api/comments \
  -H "Content-Type: application/json" \
  -d '{"content": "This is some sample text with profanity"}'
Expected Output (400 Bad Request)
{
  "id": "req_123abc",
  "isClean": false,
  "severity": "Critical",
  "categoryScores": {
    "profanity": 0.98,
    "toxicity": 0.85
  },
  "reason": "Content contains profanity"
}

5. Complete Example

Here is the full code block ready to copy and paste.

index.php
<?php

require 'vendor/autoload.php';

use SafeComms\SafeCommsClient;

// Initialize with your API Key
$client = new SafeCommsClient(getenv('SAFECOMMS_API_KEY'));

// Simple router for example purposes
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $_SERVER['REQUEST_URI'] === '/api/comments') {
    
    $input = json_decode(file_get_contents('php://input'), true);
    $content = $input['content'] ?? '';

    try {
        // 1. Check content
        $result = $client->moderateText(
            content: $content,
            // pii: true // Enable PII detection (Starter Tier+)
        );

        // 2. Act on result
        if (!$result['isClean']) {
            header('Content-Type: application/json');
            http_response_code(400);
            echo json_encode($result);
            exit;
        }

        // 3. Content is safe, proceed to save...
        // $db->query("INSERT INTO comments (content) VALUES (?)", [$content]);
        
        header('Content-Type: application/json');
        echo json_encode(['success' => true]);

    } catch (Exception $e) {
        http_response_code(500);
        echo json_encode(['error' => 'Moderation check failed']);
    }
}

Configuration & Tuning

Need to adjust sensitivity or allow certain words? You don't need to change your code. Head to the dashboard to configure your moderation profile globally.