Easily integrate with the Omni API using our PHP SDK
Install the Omni PHP SDK using Composer:
composer require omnimoderate/sdk
First, import the SDK and create a client instance with your API key:
<?php
require_once 'vendor/autoload.php';
use Omnimoderate\Client;
// Create a client instance
$client = new Client([
'api_key' => 'your_api_key_here'
]);
Here's a simple example of how to moderate text content:
<?php
// Moderate a single text item
$result = $client->moderate([
'input' => [
['type' => 'text', 'text' => 'This is a sample text to moderate.']
]
]);
echo "Moderation result: " . ($result['flagged'] ? 'Yes' : 'No') . PHP_EOL;
And here's how to moderate both text and images:
<?php
// Function to convert an image file to base64
function imageToBase64($imagePath) {
$imageData = file_get_contents($imagePath);
$base64 = base64_encode($imageData);
$mimeType = mime_content_type($imagePath);
return "data:{$mimeType};base64,{$base64}";
}
// Moderate text and image
function moderateMixedContent($textContent, $imagePath) {
global $client;
// Convert image to base64
$imageBase64 = imageToBase64($imagePath);
$result = $client->moderate([
'input' => [
['type' => 'text', 'text' => $textContent],
['type' => 'image', 'base64' => $imageBase64]
],
'analyze_images' => true
]);
echo "Moderation result: " . ($result['flagged'] ? 'Yes' : 'No') . PHP_EOL;
// Check text result
echo "Text flagged: " . ($result['results'][0]['flagged'] ? 'Yes' : 'No') . PHP_EOL;
// Check image result
echo "Image flagged: " . ($result['results'][1]['flagged'] ? 'Yes' : 'No') . PHP_EOL;
// Check vision analysis
if (isset($result['vision_analysis'])) {
echo "Image analysis: " . $result['vision_analysis'][0]['analysis'] . PHP_EOL;
}
return $result;
}
// Example usage
$result = moderateMixedContent(
"This restaurant has amazing food!",
"path/to/image.jpg"
);
The SDK automatically verifies response signatures if you provide your signature secret:
<?php
// Create a client instance with signature verification
$client = new Client([
'api_key' => 'your_api_key_here',
'signature_secret' => 'your_signature_secret_here'
]);
// The SDK will automatically verify signatures
$result = $client->moderate([
'input' => [
['type' => 'text', 'text' => 'This is a sample text to moderate.']
]
]);
The SDK includes built-in retry logic for rate limit errors:
<?php
// Create a client instance with retry options
$client = new Client([
'api_key' => 'your_api_key_here',
'max_retries' => 3,
'retry_delay' => 1.0, // 1 second
'max_retry_delay' => 10.0 // 10 seconds
]);
// The SDK will automatically retry rate-limited requests
$result = $client->moderate([
'input' => [
['type' => 'text', 'text' => 'This is a sample text to moderate.']
]
]);
You can customize the request options used by the SDK:
<?php
// Create a client instance with custom request options
$client = new Client([
'api_key' => 'your_api_key_here',
'timeout' => 10.0, // 10 seconds
'headers' => [
'User-Agent' => 'My Custom App/1.0'
]
]);
Here's an example of how to integrate the SDK with a Laravel controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Omnimoderate\Client;
class ModerationController extends Controller
{
protected $client;
public function __construct()
{
$this->client = new Client([
'api_key' => env('OMNI_API_KEY')
]);
}
public function moderate(Request $request)
{
$request->validate([
'content' => 'required|array',
'content.text' => 'sometimes|string',
'content.image' => 'sometimes|string',
'analyze_images' => 'sometimes|boolean'
]);
$content = $request->input('content');
$analyzeImages = $request->input('analyze_images', false);
// Prepare input array
$inputData = [];
// Add text if provided
if (isset($content['text']) && !empty($content['text'])) {
$inputData[] = ['type' => 'text', 'text' => $content['text']];
}
// Add image if provided
if (isset($content['image']) && !empty($content['image'])) {
$inputData[] = ['type' => 'image', 'base64' => $content['image']];
}
if (empty($inputData)) {
return response()->json(['error' => 'No valid content provided'], 400);
}
try {
// Submit for moderation
$result = $this->client->moderate([
'input' => $inputData,
'analyze_images' => $analyzeImages
]);
return response()->json($result);
} catch (Exception $e) {
return response()->json(['error' => $e->getMessage()], 500);
}
}
}