PHP code example of sarus-io / sarus-sdk-php

1. Go to this page and download the library: Download sarus-io/sarus-sdk-php 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/ */

    

sarus-io / sarus-sdk-php example snippets




use Sarus\SdkFactory;
use Sarus\Config;

$config = new Config(
   'SECRET',               // Requred  - API secret for sarus    
   'https://api.sarus.io', // Optional - base url
   30,                     // Optional - timeout
   true                    // Optional - ssl verify
);
//OR
$config = Config::fromArray([
   'secret'    => 'SECRET',               // Requred  - API secret for sarus    
   'baseUri'   => 'https://api.sarus.io', // Optional - base url
   'timeout'   => 30,                     // Optional - timeout
   'sslVerify' => true                    // Optional - ssl verify
]);
$factory = new SdkFactory();
$sdk = $factory->create($config);
 
// If you want to log request and response:
 $logger = new Logger(); //class implemented \Psr\Log\LoggerInterface
 $logFormat = \GuzzleHttp\MessageFormatter::CLF; \\Formating of log message
 $logLevel = \Psr\Log\LogLevel::INFO; //By default log level is info
 $sdk = $factory->createWithLogger($config, $logger, $logFormat, $logLevel);

try {

    $sdk->ping();

} catch (\Sarus\Client\Exception\HttpException $e) {
}


$sarusProductUuids = ['uuid1', 'uuid2'];
$user = new \Sarus\User(
    '[email protected]',
    'test_name',
    'test-last_name',
    96
);
$user
    ->setCity('Milwaukee')
    ->setCountry('USA')
    ->setRegion('Wisconsin')
    ->setAddress1('53 Creek Lane')
    ->setPostalCode('53204')
;
try {

    $sdk->purchaseProduct($sarusProductUuids, $user);

} catch (\Sarus\Client\Exception\HttpException $e) {
}


$sarusPoductUuid = 'uuid1';
try {

    $sdk->unlinkProduct($sarusPoductUuid);

} catch (\Sarus\Client\Exception\HttpException $e) {
}

$email = '[email protected]';
try {

    $response = $sdk->listEnrollments($email);
    
    foreach($response->get('data') as $enrollment) {
        $enrollment['course_uuid'];
        $enrollment['title'];
        $enrollment['description'];
        $enrollment['image_src'];
        $enrollment['url'];
    }

} catch (\Sarus\Client\Exception\HttpException $e) {
}

$email = '[email protected]';
$sarusPoductUuids = ['uuid1', 'uuid2'];
try {

    $sdk->deactivateEnrollments($email, $sarusPoductUuids);

} catch (\Sarus\Client\Exception\HttpException $e) {
}

$request = \Sarus\Request\CustomRequest::fromArray([
    'path'   => '/v1/ping',
    'method' => 'GET',
    'body'   => null,
]);

try {

    $resposne = $sdk->handleRequest($request);

} catch (\Sarus\Client\Exception\HttpException $e) {
}

$request = \Sarus\Request\CustomRequest::fromArray([
    'path'   => '/v1/ping',
    'method' => 'GET',
    'body'   => null,
]);

$serialized = serialize($request);
$unserializedRequest = unserialize($serializaed);

$request = \Sarus\Request\CustomRequest::fromArray([
    'path'   => '/v1/ping',
    'method' => 'GET',
    'body'   => null,
]);

$serialized = \json_encode($request);

$data = \json_decode($serialized, true);
$unserializedRequest = \Sarus\Request\CustomRequest::fromArray($data);

$request = \Sarus\Request\CustomRequest::fromArray([
    'path'   => '/v1/ping',
    'method' => 'GET',
    'body'   => null,
]);

$serialized = $request->toArray();
$unserializedRequest = \Sarus\Request\CustomRequest::fromArray($data);

try {

// request

} catch (\Sarus\Client\Exception\HttpException $e) {
    // The exceptions is thrown if http request is not successful

    $e->getMessage();           // returns either grabbed message from response body or reason phrase
    $e->hasResponse();          // returns bool, if exception holds response object
    $e->getResponse();          // returns \Psr\Http\Message\ResponseInterface
    $e->getRequest();           // returns  \Psr\Http\Message\RequestInterface
}
bash
composer