Skip to content

kwazaro/yii2-httpclient-requests-limit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

Yii2 Http Client requests limit behavior

This is the behavior for Yii2 Http Client, which provides rate limiting for requests per second or per minute. It is useful for connecting web services with rate limiting. This behavior uses Yii2 Redis extension to store rate-limiting info.

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist kwazaro/yii2-httpclient-requests-limit "*"

or add

"kwazaro/yii2-httpclient-requests-limit": "*"

to the require section of your composer.json file.

Usage

Once the extension is installed, simply use it in your code by :

<?php
use kwazaro\yii2\httpclient\HttpClientRateLimitBehavior;

$client = new \yii\httpclient\Client([
    'baseUrl' => 'https://example.com/api',
]);
$client->attachBehavior('requestsLimit', [
    'class' => HttpClientRateLimitBehavior::class,
    'redis' => 'redis', // ID of your Yii2 Redis component.
    'redisKey' => 'myRequests', // Name of Redis key for storing data.
    'maxRequestsPerSecond' => 10, // Max number of requests per second.
    'maxRequestsPerMinute' => 100, // Max number of requests per minute.
]);

About

Behavior to limit requests for Yii2 Http Client

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages