classy-org / classy-php-sdk
Php HTTP client library for Classy API
Installs: 18 609
Dependents: 0
Suggesters: 0
Security: 0
Stars: 9
Watchers: 35
Forks: 3
Open Issues: 4
Requires
- guzzlehttp/guzzle: ^6.2
Requires (Dev)
- mockery/mockery: ^0.9.4
- phpunit/phpunit: ~4.0
README
This repository contains a php HTTP client library to let your php App interact with Classy's API.
Installation
The Classy PHP SDK can be installed with Composer:
composer require classy-org/classy-php-sdk
Be sure you included composer autoloader in your app:
require_once '/path/to/your/project/vendor/autoload.php';
Usage
Basic example
$client = new \Classy\Client([ 'client_id' => 'your_client_id', 'client_secret' => 'your_client_secret', 'version' => '2.0' // version of the API to be used ]); $session = $client->newAppSession(); // Get information regarding the campaign #1234 $campaign = $client->get('/campaigns/1234', $session); // Access the campaign goal: $campaign->goal // Unpublish the campaign $client->post('/campaign/1234/deactivate', $session);
Sessions handling
Sessions have an expiration date. It is possible to refresh a session:
if ($session->expired()) { $client->refresh($session) } // $session->expired() is now false.
Sessions are serializable, they can be saved an reused to reduce the amount of API calls:
$client = new \Classy\Client([ 'client_id' => 'your_client_id', 'client_secret' => 'your_client_secret', 'version' => '2.0' // version of the API to be used ]); // Retrieve the session from a file $session = unserialize(file_get_contents("path/to/a/cache/file")); // ... work with the API... // Save the session for later file_put_contents("path/to/a/cache/file", serialize($session));
Errors handling
This client can throw two types of Exceptions:
- Classy\Exceptions\SDKException when the SDK is misused
- Classy\Exceptions\APIResponseException when the API is not returning an OK response
try { $response = $client->get('/endpoint', $session); } catch (\Classy\Exceptions\APIResponseException $e) { // Get the HTTP response code $code = $e->getCode(); // Get the response content $content = $e->getResponseData(); // Get the response headers $headers = $e->getResponseHeaders(); }