> ## Documentation Index
> Fetch the complete documentation index at: https://docs.moderationapi.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Content moderation with PHP

> Learn how to moderate content using Moderation API's PHP SDK

***

<Snippet file="prerequisites.mdx" />

***

## 1. Install the SDK

Install the official PHP SDK via Composer:

```bash theme={"theme":"nord"}
composer require moderation-api/sdk-php
```

<Note>
  Requires PHP 8.1.0 or higher. The SDK is currently in beta.
</Note>

## 2. Submit content

Grab the API key from your [project](https://dash.moderationapi.com) and begin submitting text, images, or other media to your project for moderation.

<CodeGroup>
  ```php Text moderation theme={"theme":"nord"}
  <?php
  use ModerationAPI\Client;

  // Initialize the client with your secret key
  $client = new Client(secretKey: getenv('MODAPI_SECRET_KEY') ?: 'your-api-key');

  // Submit text for moderation
  $response = $client->content->submit([
      'content' => [
          'type' => 'text',
          'text' => 'Hello world!'
      ],
      'authorId' => '123',
      'conversationId' => '456',
      'metadata' => [
          'customField' => 'value'
      ]
  ]);

  // Check if content was flagged
  if ($response->evaluation->flagged) {
      echo "Content was flagged\n";
  }

  // Act on the recommendation
  switch ($response->recommendation->action) {
      case 'allow':
          echo "Text content is safe.\n";
          // Save to database or proceed...
          break;
      case 'review':
          echo "Text content needs review\n";
          // Send to review queue
          break;
      case 'reject':
          echo "Text content rejected\n";
          // Block the content, show an error, etc...
          break;
  }
  ```

  ```php Image moderation theme={"theme":"nord"}
  <?php
  use ModerationAPI\Client;

  // Initialize the client with your secret key
  $client = new Client(secretKey: getenv('MODAPI_SECRET_KEY') ?: 'your-api-key');

  // Submit image for moderation
  $response = $client->content->submit([
      'content' => [
          'type' => 'image',
          'url' => 'https://example.com/image.jpg'
      ],
      'authorId' => '123',
      'conversationId' => '456',
      'metadata' => [
          'customField' => 'value'
      ]
  ]);

  // Check if content was flagged
  if ($response->evaluation->flagged) {
      echo "Image was flagged\n";
  }

  // Act on the recommendation
  switch ($response->recommendation->action) {
      case 'allow':
          echo "Image is safe.\n";
          // Save to database or proceed...
          break;
      case 'review':
          echo "Image needs review\n";
          // Send to review queue
          break;
      case 'reject':
          echo "Image rejected\n";
          // Block or require review
          break;
  }
  ```

  ```php Object moderation theme={"theme":"nord"}
  <?php
  use ModerationAPI\Client;

  // Initialize the client with your secret key
  $client = new Client(secretKey: getenv('MODAPI_SECRET_KEY') ?: 'your-api-key');

  // Submit object for moderation
  $response = $client->content->submit([
      'content' => [
          'type' => 'object',
          'data' => [
              'name' => [
                  'type' => 'text',
                  'text' => 'John Doe'
              ],
              'email' => [
                  'type' => 'text',
                  'text' => 'john.doe@example.com'
              ],
              'picture' => [
                  'type' => 'image',
                  'url' => 'https://example.com/image.jpg'
              ]
          ]
      ],
      'authorId' => '123',
      'conversationId' => '456',
      'metadata' => [
          'customField' => 'value'
      ]
  ]);

  // Check if content was flagged
  if ($response->evaluation->flagged) {
      echo "Object was flagged\n";
  }

  // Act on the recommendation
  switch ($response->recommendation->action) {
      case 'allow':
          echo "Object is safe.\n";
          // Save to database or proceed...
          break;
      case 'review':
          echo "Object needs review\n";
          // Send to review queue
          break;
      case 'reject':
          echo "Object rejected\n";
          // Block or require review
          break;
  }
  ```
</CodeGroup>

## 3. Handle errors

The SDK throws specific exception types for different error scenarios:

```php theme={"theme":"nord"}
<?php
use ModerationAPI\Client;
use ModerationAPI\Core\Exceptions\APIConnectionException;
use ModerationAPI\Core\Exceptions\BadRequestException;
use ModerationAPI\Core\Exceptions\AuthenticationException;
use ModerationAPI\Core\Exceptions\RateLimitException;

$client = new Client(secretKey: getenv('MODAPI_SECRET_KEY') ?: 'your-api-key');

try {
    $response = $client->content->submit([
        'content' => [
            'type' => 'text',
            'text' => 'Hello world!'
        ]
    ]);

    echo "Recommendation: " . $response->recommendation->action . "\n";

} catch (BadRequestException $e) {
    echo "Invalid request: " . $e->getMessage();
} catch (AuthenticationException $e) {
    echo "Authentication failed: " . $e->getMessage();
} catch (RateLimitException $e) {
    echo "Rate limit exceeded: " . $e->getMessage();
} catch (APIConnectionException $e) {
    echo "Connection error: " . $e->getMessage();
}
```

<Note>
  The SDK automatically retries failed requests up to 2 times by default. You can configure retries globally when initializing the client with `maxRetries: 0` or per-request using `RequestOptions::with(maxRetries: 5)`.
</Note>

***

<Snippet file="dry-run.mdx" />

***

<Snippet file="review-flagged.mdx" />

***

<Snippet file="all-done.mdx" />
