PHP code example of gigerit / bexio-api-client

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

    

gigerit / bexio-api-client example snippets


use Bexio\BexioClient;
use Bexio\Resources\Contact;

$client = new BexioClient('API_TOKEN');

// Get the Contact with ID 1
$contact = Contact::useClient($client)->find(1); 

// Access the Contact properties
echo $contact->id;
echo $contact->name_1;
echo $contact->mail;

use Bexio\BexioClient;
use Bexio\Resources\Contact;

$client = new BexioClient('API_TOKEN');

// Get all Contacts
$contacts = Contact::useClient($client)->all();

// Access the Contacts
foreach ($contacts as $contact) {
    echo $contact->id;
    echo $contact->name_1;
    echo $contact->mail;
}

use Bexio\BexioClient;
use Bexio\Resources\Contact;

$client = new BexioClient('API_TOKEN');

// Create a new Contact
$contact = new Contact(
    name_1: 'John Doe',
);

// Save the Contact
$contact->attachClient($client)->save();

use Bexio\BexioClient;
use Bexio\Resources\Contact;

$client = new BexioClient('API_TOKEN');

// Get the Contact with the ID 1
$contact = Contact::useClient($client)->find(1); 

// Access the Contact properties
echo $contact->id;
echo $contact->name_1;
echo $contact->mail;

// Simply update the Contact properties
$contact->name_1 = 'Jane Doe';

// Send the changes back to bexio
$contact->save();

use Bexio\BexioAuth;

//Provided from https://developer.bexio.com/
$auth = new BexioAuth(
    'CLIENT_ID',
    'CLIENT_SECRET',
    'http://localhost/bexio/callback'
);

$url = $auth->getAuthorizationUrl(
    scopes: [
        "company_profile",
        "email",
        "offline_access",
        "openid",
        "profile",
    ],
    state: 'random-state-string'
);

header('Location: ' . $url);

use Bexio\BexioAuth;

$code = $_GET['code'];
$state = $_GET['state'];


$auth = new BexioAuth(
    'CLIENT_ID',
    'CLIENT_SECRET',
    'http://localhost/bexio/callback'
);

$oauthAuthenticator = $auth->getAccessToken($code, $state, 'random-state-string');

// ----------------------------------------
// Your logic to store the access token and refresh token
// (e.g. in a database, you can just serialize the $oauthAuthenticator object for example)
// ----------------------------------------


use Saloon\Http\Auth\AccessTokenAuthenticator;
use Bexio\BexioClient;
use Bexio\BexioAuth;
    
// ----------------------------------------
// Your logic to retrieve the access token and refresh token
// ----------------------------------------

//create a AccessTokenAuthenticator object or unserialize it from your store/database
$auth = new AccessTokenAuthenticator(
    $yourDatastore->access_token,
    $yourDatastore->refresh_token,
    $yourDatastore->access_token_expires_at //as DateTimeImmutable
);

if ($auth->hasExpired()) {
    $auth = BexioAuth::make()->refreshAccessToken($auth);
   
   // ----------------------------------------
   // Your logic to store the new access token and refresh token
   // ----------------------------------------
}

$client = new BexioClient($auth->getAccessToken());

// Use the client to interact with the bexio API