A simple client library to remotely access the Sailthru REST API
. By default, it will make requests in JSON
format.
You can clone via GitHub or install via composer.
git clone [email protected]:sailthru/sailthru-php5-client.git
or
composer require sailthru/sailthru-php5-client
For basic usage, you can initialize with just API Key and Secret
$client = new Sailthru_Client($api_key, $api_secret);
As of 2.0.0, the client library will throw a Sailthru_Client_Exception
on API and IO errors, which should be properly handled.
Error codes 1000, 1001, 10002 are IO-related, while 0-99 and XX are API errors.
try {
$client->apiPost('user', [..]);
} catch (Sailthru_Client_Exception $e) {
$code = $e->getCode();
$message = $->getMessage();
// process error
}
Increase timeout from 10 (default) to 30 seconds.
$client = new Sailthru_Client($this->api_key, $this->secret, $this->api_url, array('timeout' => 30000, 'connect_timeout' => 30000));
Below shows an example of how to check for Sailthru API rate limiting and throttle requests based on that information.
// get last rate limit info
$rate_limit_info = $sailthru_client->getLastRateLimitInfo("user", "POST");
// getRateLimitInfo returns null if given endpoint/method wasn't triggered previously
if ($rate_limit_info) {
$limit = $rate_limit_info['limit'];
$remaining = $rate_limit_info['remaining'];
$reset_timestamp = $rate_limit_info['reset'];
// throttle api calls based on last rate limit info
if ($remaining <= 0) {
$seconds_till_reset = $reset_timestamp - time();
// sleep or perform other business logic before next user api call
sleep($seconds_till_reset);
}
}
You can run the tests locally with composer and phpunit:
cd sailthru-php5-client
composer install
vendor/bin/phpunit