PHP code example of ziya / yii2-redis-rate-limiter

1. Go to this page and download the library: Download ziya/yii2-redis-rate-limiter 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/ */

    

ziya / yii2-redis-rate-limiter example snippets


use ziya\RedisRateLimiter\exceptions\LimitExceeded;
use ziya\RedisRateLimiter\Limiter;

$key = 'api_request_{user_id}'; //Unique key identifier
$count = 60; // Request count in given amount of time
$interval = 60; //Request interval time
$limiter = new Limiter($key,$count,$interval);
//Set connection is optional. By default it get connection from Yii::$app->redis
// $limiter->setConnection(Yii::$app->redis);

try {
    $limiter->limit();    
    //limit not exceeded
} catch (LimitExceeded $exception) {
   //limit exceeded
}