GithubHelp home page GithubHelp logo

ubicx / bexio-api-php-client Goto Github PK

View Code? Open in Web Editor NEW

This project forked from christianruhstaller/bexio-api-php-client

0.0 1.0 0.0 29 KB

bexio API Library for PHP

License: MIT License

PHP 100.00%

bexio-api-php-client's Introduction

bexio API PHP Client

The bexio API Client Library enables you to work with the bexio API. This is an early version and is still in development.

See the bexio API documentation for more information how to use the API.

Installation

You can use Composer or download the library.

Require this package with composer:

composer require christianruhstaller/bexio-api-php-client

Include the autoloader:

require_once '/path/to/your-project/vendor/autoload.php';

Examples

Get access token

require_once '../vendor/autoload.php';

$clientId = '9999999999999.apps.bexio.com'; // The client id you have received from the bexio support
$clientSecret = 'W1diwrEvHlgQMPRYdr3t6I1z5sQ='; // The client secret you have received from the bexio support
$redirectUri = 'http://localhost/bexio-api-php-client.php'; // Set here your Url where this script gets called
$scope = 'general'; // A whitespace-separated list of scopes (see https://docs.bexio.com/oauth/scopes/).
$state = '8OTs2JTDcWDaPqV7o9aHVWqM'; // A random sequence. Should be used as a protection against CSRF-Attacks
$credentialsPath = 'client_credentials.json'; // Set the path where the credentials file will be stored

$curl = new \Curl\Curl();

$client = new \Bexio\Client(
    [
        'clientId'     => $clientId,
        'clientSecret' => $clientSecret,
    ]
);
$client->setRedirectUri($redirectUri);

// If code is not set we need to get the authentication code
if (!isset($_GET['code'])) {
    $redirectTo = \Bexio\Client::OAUTH2_AUTH_URL.'?'.http_build_query(
            [
                'client_id'     => $clientId,
                'client_secret' => $clientSecret,
                'redirect_uri'  => $redirectUri,
                'scope'         => $scope,
                'state'         => $state,
            ]
        );

    header('Location: '.$redirectTo);
    exit;
} else {
    $accessToken = $client->fetchAccessTokenWithAuthCode($_GET['code']);
    file_put_contents($credentialsFile, json_encode($accessToken));
    exit;
}

Init client

    require_once '../vendor/autoload.php';
    
    $client = new \Bexio\Client([
        'clientId' => 'CLIENT_ID',
        'clientSecret' => 'CLIENT_SECRET',
    ]);
    
    $credentialsPath = 'PATH_TO_CREDENTIAL_FILE';
    
    if (!file_exists($credentialsPath)) {
        throw new \Exception('Credentials file not found for OAuth: '.$credentialsPath);
    }

    $accessToken = file_get_contents($credentialsPath);
    $client->setAccessToken($accessToken);

    if ($client->isAccessTokenExpired()) {
        $client->refreshToken($client->getRefreshToken());
        file_put_contents($credentialsPath, $client->getAccessToken());
    }

Get contacts

    $bexio = new \Bexio\Resource\Contact($client);
    
    $contacts = $bexio->getContacts();

bexio-api-php-client's People

Contributors

christianruhstaller avatar cbaconnier avatar tomasz-silpion avatar ubicx avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.