Reusing authentication tokens - 2Parale/2Performant-php GitHub Wiki

Context

When you instantiate a new \TPerformant\API\HTTP\Affiliate or \TPerformant\API\HTTP\Advertiser object, using the $email and $password parameters, the code asks the 2Performant API server to create a new authentication token.

2Performant's API server stores a limited number of authentication tokens for each user; therefore, it destroys the oldest ones when new authentication requests are made.

This means that, if you make a large number of API authentication requests at once, there is a chance that you can be logged out and get a HTTP 401 Unauthorized response.

Solution

You should authenticate once and then do all your data handling without further authentication. This API wrapper automatically refreshes the authentication tokens with the new ones the server provides, keeping you logged in.

Example:

use TPerformant\API\HTTP\Advertiser;
use TPerformant\API\Filter\AdvertiserCommissionFilter;

$me = new Advertiser('[email protected]', 'secretpassword');

// get first 5 pages of commissions
for($i = 1; $i <= 5; $i++) {
    $commissions = $me->getCommissions((new AdvertiserCommissionFilter)->page($i));
    // display commissions
}