PHP code example of select-co / sage200-api-client

1. Go to this page and download the library: Download select-co/sage200-api-client 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/ */

    

select-co / sage200-api-client example snippets




use SelectCo\Sage200ApiClient\Sage200Connector;
use SelectCo\Sage200ApiClient\Http\Request;
use SelectCo\Sage200ApiClient\QueryBuilder\SageODataBuilder;
use SelectCo\Sage200ApiClient\Exception\SageException;

 // or e.g. 'mysite'
    xCompany: null, // or e.g. 'mycompany'
);

// Optionally set site/company later
$connector->setSite('mysite');
$connector->setCompany('mycompany');

// Build an OData query: /customers?$select=id,reference,name&$top=10
$query = (new SageODataBuilder())
    ->select(['id', 'reference', 'name'])
    ->top(10);

// Create a simple request by extending the base Request class
class ListCustomers extends Request
{
    protected $method = 'GET';
    protected $endPoint = '/customers';
}

$request = new ListCustomers();
$request->setQueryParameters($query);

try {
    $response = $connector->send($request);

    // Access raw contents and decode JSON
    $contents = $response->getContents();
    $json = json_decode($contents, true, 512, JSON_THROW_ON_ERROR);
    print_r($json);

    // Other helpers
    $status = $response->getStatus();
    $headers = $response->getResponse()->getHeaders();
} catch (SageException $e) {
    // One of: BadRequestException, UnauthorizedException, ForbiddenException, NotFoundException
    // Contains the raw Sage response body for debugging
    echo $e->getMessage() . PHP_EOL;
    echo $e->getSageResponse() . PHP_EOL;
}

class CreateCustomer extends Request {
    protected $method = 'POST';
    protected $endPoint = '/customers';

    public function __construct(array $payload)
    {
        $this->body = json_encode($payload, JSON_THROW_ON_ERROR);
    }
}

$req = new CreateCustomer([
    'reference' => 'ACME01',
    'name' => 'ACME, Inc.',
]);
$response = $connector->send($req);

$query = (new SageODataBuilder())
    ->select(['id', 'reference', 'name'])
    ->filter('reference', 'eq', "ACME01")
    ->andFilter('balance', 'gt', 1000)
    ->orderBy('name', 'asc')
    ->top(50)
    ->skip(100)
    ->count(true);

$request->setQueryParameters($query);

$resources = $connector->resources();
// With endpoints package present, this might expose, for example:
// $resources->customers()->list($query); // illustrative only