PHP code example of traderinteractive / tol-api

1. Go to this page and download the library: Download traderinteractive/tol-api 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/ */

    

traderinteractive / tol-api example snippets


use TraderInteractive\Api;
$apiAdapter = new Api\GuzzleAdapter();
$auth = Api\Authentication::createClientCredentials(
    'clientId',
    'clientSecret'
)
$apiClient = new Api\Client(
    $apiAdapter,
    $auth,
    'https://baseApiUrl/v1'
);

<li>

$response = $apiClient->index(
    'resourceName',
    array('aFilter' => '5')
);

if ($response->getStatusCode() !== 200) {
    throw new Exception('Non successful index call');
}

$body = json_decode($response->getBody(), true);
$total = $body['pagination']['total'];

// Loop over the first page of items
foreach ($body['result'] as $item) {
    echo "<li>{$item['foo']}</li>\n";
}

// Get item 1234
$response = $apiClient->get('resourceName', '1234');

if ($response->getStatusCode() !== 200) {
    throw new Exception('Failed to fetch item 1234');
}

$item = json_decode($response->getBody(), true);
echo "Fetched item {$item['foo']}\n";

$response = $apiClient->post(
    'resourceName',
    array(
        'foo' => array(
            'bar' => 'boo',
            'bing' => '5',
        ),
    )
);

if ($response->getStatusCode() !== 201) {
    throw new Exception('Failed to create item foo');
}

$item = json_decode($response->getBody(), true);
echo $item['result']['foo'];

// Set item 1234's foo to bar.
$response = $apiClient->put(
    'resourceName',
    '1234',
    array('bing' => array('foo' => 'bar'))
);

if ($response->getStatusCode() !== 200) {
    throw new Exception('Failed to update item 1234');
}

// Delete item 1234.
$response = $apiClient->delete('resourceName', '1234');

if ($response->getStatusCode() !== 204) {
    throw new Exception('Failed to delete item 1234');
}

$handleOne = $apiClient->startGet('resourceName', '1234');
$handleTwo = $apiClient->startGet('resourceName', '5678');

$responseOne = $apiClient->end($handleOne);
$responseTwo = $apiClient->end($handleTwo);

if ($responseOne->getStatusCode() !== 200) {
    throw new Exception('Failed to fetch item 1234');
}

if ($responseTwo->getStatusCode() !== 200) {
    throw new Exception('Failed to fetch item 5678');
}

$itemOne = json_decode($responseOne->getBody(), true);
$itemTwo = json_decode($responseTwo->getBody(), true);

echo "Fetched item {$itemOne['foo']}\n";
echo "Fetched item {$itemTwo['foo']}\n";

<ul>

$items = new \TraderInteractive\Api\Collection(
    $apiClient,
    'resourceName',
    array('aFilter' => '5')
);
foreach ($items as $item) {
    echo "<li>{$item['foo']}</li>\n";
}