PHP code example of tillio-crm / oauth-resources

1. Go to this page and download the library: Download tillio-crm/oauth-resources 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/ */

    

tillio-crm / oauth-resources example snippets


use TillioCrm\OAuth\Client\Client;
use TillioCrm\OAuth\Resources\ResourcesClient;

$oauth = new Client([
    'clientId'     => '...',
    'clientSecret' => '...',
    'redirectUri'  => 'https://twoja-apka.pl/callback',
    'scopes'       => ['profile', 'email', 'offline_access', 'app_storage'],
]);

$resources = new ResourcesClient([
    'accessToken' => fn () => $oauth->accessToken(),
    // 'server' => 'https://auth.tillio.app', // default
]);

$storage = $resources->storage();

// Zapis (upsert — pełne nadpisanie)
$storage->put('sync_state', 'wfirma', [
    'last_run_at' => '2026-07-24T03:00:00Z',
    'cursor'      => 18234,
    'status'      => 'ok',
]);

// Odczyt
$doc = $storage->get('sync_state', 'wfirma');   // NotFoundException gdy brak
$doc = $storage->find('sync_state', 'wfirma');  // null gdy brak
echo $doc['data']['status'];                     // 'ok'

// Lista z filtrem i paginacją
$page = $storage->list('invoices', [
    'limit'  => 100,
    'filter' => ['status', '==', 'pending'],
]);
$next = $storage->list('invoices', ['after' => $page['next_cursor']]);

// Iteracja po całej kolekcji (paginacja automatyczna)
foreach ($storage->all('invoices') as $invoice) {
    // ...
}

// Usunięcie
$storage->delete('sync_state', 'wfirma');

$files = $resources->files();

// Upload / download w jednej linii
$files->upload('invoices/2026/07/faktura-123.pdf', $pdfBytes, 'application/pdf');
$pdf = $files->download('invoices/2026/07/faktura-123.pdf');

// Signed URL np. dla przeglądarki użytkownika (ważny 15 minut)
$grant = $files->downloadUrl('invoices/2026/07/faktura-123.pdf');
header('Location: ' . $grant['url']);

// Listing i kasowanie
$list = $files->list('invoices/2026/07/');
$files->delete('invoices/2026/07/faktura-123.pdf');

use TillioCrm\OAuth\Resources\Exception\NotFoundException;

try {
    $doc = $resources->storage()->get('sync_state', 'wfirma');
} catch (NotFoundException) {
    // pierwsza synchronizacja — brak stanu
}