PHP code example of samescolas / rate-limit

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

    

samescolas / rate-limit example snippets


use RateLimit\Exception\LimitExceeded;
use RateLimit\Rate;
use RateLimit\RedisRateLimiter;
use Redis;

$rateLimiter = new RedisRateLimiter(new Redis());

$apiKey = 'abc123';

try {
    $rateLimiter->limit($apiKey, Rate::perMinute(100));
    
    //on success
} catch (LimitExceeded $exception) {
   //on limit exceeded
}

use RateLimit\Rate;
use RateLimit\RedisRateLimiter;
use Redis;

$rateLimiter = new RedisRateLimiter(new Redis());

$ipAddress = '192.168.1.2';
$status = $rateLimiter->limitSilently($ipAddress, Rate::perMinute(100));

echo $status->getRemainingAttempts(); //99