|
| 1 | +# Moderyo PHP SDK |
| 2 | + |
| 3 | +Official PHP SDK for the Moderyo Content Moderation API. |
| 4 | + |
| 5 | +[](https://php.net) |
| 6 | +[](https://packagist.org/packages/moderyo/sdk) |
| 7 | +[](LICENSE) |
| 8 | + |
| 9 | +## Requirements |
| 10 | + |
| 11 | +- PHP 8.4 or higher |
| 12 | +- Guzzle HTTP 7.x |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +```bash |
| 17 | +composer require moderyo/sdk |
| 18 | +``` |
| 19 | + |
| 20 | +## Quick Start |
| 21 | + |
| 22 | +```php |
| 23 | +use Moderyo\ModeryoClient; |
| 24 | + |
| 25 | +$client = new ModeryoClient('your-api-key'); |
| 26 | + |
| 27 | +$result = $client->moderate('Hello, this is a friendly message!'); |
| 28 | + |
| 29 | +if ($result->isBlocked) { |
| 30 | + echo "BLOCKED: " . ($result->policyDecision?->reason ?? 'Policy violation') . "\n"; |
| 31 | +} elseif ($result->isFlagged) { |
| 32 | + echo "FLAGGED for review\n"; |
| 33 | +} else { |
| 34 | + echo "ALLOWED\n"; |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +## Configuration |
| 39 | + |
| 40 | +### API Key String |
| 41 | + |
| 42 | +```php |
| 43 | +$client = new ModeryoClient('your-api-key'); |
| 44 | +``` |
| 45 | + |
| 46 | +### API Key with Options |
| 47 | + |
| 48 | +```php |
| 49 | +$client = new ModeryoClient('your-api-key', [ |
| 50 | + 'baseUrl' => 'https://api.moderyo.com', |
| 51 | + 'timeout' => 60, |
| 52 | + 'maxRetries' => 5, |
| 53 | +]); |
| 54 | +``` |
| 55 | + |
| 56 | +### ModeryoConfig Object |
| 57 | + |
| 58 | +```php |
| 59 | +use Moderyo\ModeryoConfig; |
| 60 | +use Moderyo\ModeryoClient; |
| 61 | + |
| 62 | +$config = new ModeryoConfig([ |
| 63 | + 'apiKey' => 'your-api-key', |
| 64 | + 'baseUrl' => 'https://api.moderyo.com', |
| 65 | + 'timeout' => 30, |
| 66 | + 'maxRetries' => 3, |
| 67 | + 'retryDelay' => 1.0, |
| 68 | + 'defaultModel' => 'omni-moderation-latest', |
| 69 | +]); |
| 70 | + |
| 71 | +$client = new ModeryoClient($config); |
| 72 | +``` |
| 73 | + |
| 74 | +### Environment Variables |
| 75 | + |
| 76 | +```php |
| 77 | +// Set MODERYO_API_KEY and optionally MODERYO_BASE_URL |
| 78 | +$client = ModeryoClient::fromEnv(); |
| 79 | +``` |
| 80 | + |
| 81 | +## Moderation |
| 82 | + |
| 83 | +### Basic |
| 84 | + |
| 85 | +```php |
| 86 | +$result = $client->moderate('Text to check'); |
| 87 | + |
| 88 | +echo "Blocked: " . ($result->isBlocked ? 'Yes' : 'No') . "\n"; |
| 89 | +echo "Flagged: " . ($result->isFlagged ? 'Yes' : 'No') . "\n"; |
| 90 | +echo "Allowed: " . ($result->isAllowed ? 'Yes' : 'No') . "\n"; |
| 91 | +``` |
| 92 | + |
| 93 | +### With Options |
| 94 | + |
| 95 | +```php |
| 96 | +$result = $client->moderate('Text to check', [ |
| 97 | + 'model' => 'omni-moderation-latest', |
| 98 | + 'longTextMode' => true, |
| 99 | + 'mode' => 'enforce', |
| 100 | + 'risk' => 'balanced', |
| 101 | +]); |
| 102 | +``` |
| 103 | + |
| 104 | +### Batch |
| 105 | + |
| 106 | +```php |
| 107 | +$batch = $client->moderateBatch(['Hello', 'Bad text', 'Spam']); |
| 108 | +echo "Blocked: " . count($batch->getBlocked()) . "\n"; |
| 109 | +echo "Has blocked: " . ($batch->hasBlocked() ? 'Yes' : 'No') . "\n"; |
| 110 | +``` |
| 111 | + |
| 112 | +## Error Handling |
| 113 | + |
| 114 | +```php |
| 115 | +use Moderyo\Exceptions\AuthenticationException; |
| 116 | +use Moderyo\Exceptions\RateLimitException; |
| 117 | +use Moderyo\Exceptions\ValidationException; |
| 118 | +use Moderyo\Exceptions\QuotaExceededException; |
| 119 | +use Moderyo\Exceptions\NetworkException; |
| 120 | +use Moderyo\Exceptions\ModeryoException; |
| 121 | + |
| 122 | +try { |
| 123 | + $result = $client->moderate($text); |
| 124 | +} catch (AuthenticationException $e) { |
| 125 | + // Invalid API key (401) |
| 126 | +} catch (RateLimitException $e) { |
| 127 | + sleep((int) $e->retryAfter); |
| 128 | +} catch (ValidationException $e) { |
| 129 | + // Invalid input (400/422) |
| 130 | +} catch (QuotaExceededException $e) { |
| 131 | + // Plan quota exceeded (402) |
| 132 | +} catch (NetworkException $e) { |
| 133 | + // Connection/timeout after retries |
| 134 | +} catch (ModeryoException $e) { |
| 135 | + echo "Error: " . $e->getMessage() . "\n"; |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +## Laravel Integration |
| 140 | + |
| 141 | +Add MODERYO_API_KEY=your-key to .env. Service provider auto-discovers. |
| 142 | + |
| 143 | +```php |
| 144 | +use Moderyo\Laravel\Facades\Moderyo; |
| 145 | +$result = Moderyo::moderate('Check this text'); |
| 146 | +``` |
| 147 | + |
| 148 | +## Running Tests |
| 149 | + |
| 150 | +```bash |
| 151 | +composer install |
| 152 | +vendor/bin/phpunit |
| 153 | +``` |
| 154 | + |
| 155 | +## License |
| 156 | + |
| 157 | +MIT - see [LICENSE](LICENSE). |
0 commit comments