PHP code example of spamtroll / php-sdk

1. Go to this page and download the library: Download spamtroll/php-sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

spamtroll / php-sdk example snippets




use Spamtroll\Sdk\Client;
use Spamtroll\Sdk\Request\CheckSpamRequest;

$client = new Client('your-api-key');

$response = $client->checkSpamOrHam(
    new CheckSpamRequest(
        content: $comment,
        source: CheckSpamRequest::SOURCE_COMMENT,
        ipAddress: $_SERVER['REMOTE_ADDR'] ?? null,
        username: $author,
        email: $authorEmail,
    )
);

if ($response->wasSkipped()) {
    error_log('spamtroll: skipped (' . $response->getSkipReason() . ')');
    // fail open — let the content through
} elseif ($response->shouldBlock()) {
    // block
} elseif ($response->shouldModerate()) {
    // send to moderation
}

use Spamtroll\Sdk\Client;
use Spamtroll\Sdk\ClientConfig;

$config = new ClientConfig(
    baseUrl: 'https://api.spamtroll.io/api/v1',
    timeout: 3,
    maxRetries: 2,
    retryBaseDelayMs: 250,
    userAgent: 'my-plugin/1.0 spamtroll-php-sdk/' . \Spamtroll\Sdk\Version::VERSION,
    scoreDenominator: 30.0,
    totalBudgetMs: 6000,
);

$client = new Client('your-api-key', $config);

use Spamtroll\Sdk\Client;
use Spamtroll\Sdk\Http\HttpClientInterface;
use Spamtroll\Sdk\Http\HttpResponse;
use Spamtroll\Sdk\Exception\ConnectionException;
use Spamtroll\Sdk\Exception\TimeoutException;

final class MyHttpClient implements HttpClientInterface
{
    public function send(string $method, string $url, array $headers, ?string $body, int $timeout): HttpResponse
    {
        // 1. Translate every transport failure into
        //    ConnectionException / TimeoutException. Catch \Throwable.
        // 2. 4xx/5xx are NOT errors here — return them via HttpResponse.
        // 3. Never follow redirects: they carry X-API-Key to another host.
    }
}

$client = new Client('your-api-key', null, new MyHttpClient());
bash
composer