PHP code example of reddingwebdev / abuseipdb

1. Go to this page and download the library: Download reddingwebdev/abuseipdb 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/ */

    

reddingwebdev / abuseipdb example snippets


use AbuseIpDb\AbuseIpDbClient;

$client = new AbuseIpDbClient('YOUR_API_KEY');

$result = $client->check('8.8.8.8', 90, true); // ip, maxAgeInDays, verbose
$result->getAbuseConfidenceScore();
$result->getIsp();
$result->getCountryCode();
$result->getReports(); // populated when verbose

$page = $client->reports('118.25.6.39', 30, 1, 25); // ip, maxAgeInDays, page, perPage
$page->getTotal();
$page->hasNextPage();
foreach ($page->getResults() as $report) { /* AbuseIpDb\Result\Report */ }

use AbuseIpDb\Request\BlacklistParameters;

// confidenceMinimum, limit, onlyCountries, exceptCountries, ipVersion, plaintext
$list = $client->blacklist(new BlacklistParameters(90, 10000, ['US'], [], 4));
foreach ($list as $entry) {
    echo $entry->getIpAddress(), ' ', $entry->getAbuseConfidenceScore(), PHP_EOL;
}

// Plaintext mode returns the raw newline-separated string
$text = $client->blacklist(new BlacklistParameters(null, null, [], [], null, true));

use AbuseIpDb\Enum\ReportCategory;

$result = $client->report(
    '127.0.0.1',
    [ReportCategory::SSH, ReportCategory::BRUTE_FORCE],
    'SSH login attempts with user root.',
    new \DateTimeImmutable()
);
$result->getAbuseConfidenceScore();

$block = $client->checkBlock('127.0.0.1/24', 15);
foreach ($block->getReportedAddresses() as $address) {
    echo $address->getIpAddress(), ': ', $address->getNumReports(), PHP_EOL;
}

$result = $client->bulkReport('/path/to/report.csv'); // file path or raw CSV contents
$result->getSavedReports();
foreach ($result->getInvalidReports() as $error) {
    echo $error->getRowNumber(), ': ', $error->getError(), PHP_EOL;
}

$result = $client->clearAddress('127.0.0.1');
$result->getIpAddress();
$result->getNumReportsDeleted();

use AbuseIpDb\Exception\AbuseIpDbException;
use AbuseIpDb\Exception\RateLimitExceededException;

try {
    $result = $client->check($ip);
} catch (RateLimitExceededException $e) {
    sleep($e->getRetryAfter() ?? 60);
} catch (AbuseIpDbException $e) {
    // log and fail open/closed
}

$client->check($ip);
$rateLimit = $client->getLastRateLimit(); // null before the first call
$rateLimit->getLimit();
$rateLimit->getRemaining();
$rateLimit->getResetAt(); // ?DateTimeImmutable

use AbuseIpDb\AbuseIpDbClient;

$client = new AbuseIpDbClient('YOUR_API_KEY', $psr18Client, $requestFactory, $streamFactory, [
    'logger' => $psr3Logger,           // optional PSR-3 logger
    'default_max_age_in_days' => 30,   // used by check/reports/checkBlock when maxAgeInDays is omitted
    'base_uri' => 'https://api.abuseipdb.com/api/v2/',
]);

// config/bundles.php
return [
    // ...
    AbuseIpDb\Bridge\Symfony\AbuseIpDbBundle::class => ['all' => true],
];

use AbuseIpDb\AbuseIpDbClient;

final class IpGuard
{
    public function __construct(private AbuseIpDbClient $abuseIpDb) {}
}

final class AbuseReportCommand
{
    public function __construct(private string $abuseIpDbKey) {}
}

  use Symfony\Component\DependencyInjection\Attribute\Autowire;

  final class AbuseReportCommand
  {
      public function __construct(
          #[Autowire('%abuse_ip_db.api_key%')] private string $abuseIpDbKey
      ) {}
  }