Skip to content

Commit f93d5f8

Browse files
authored
Merge pull request #1 from onlime/bexio-api-v3
Bexio API v3 support
2 parents c166826 + 6b4b574 commit f93d5f8

22 files changed

+925
-788
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
~*
22
vendor
3-
composer.lock
3+
composer.lock
4+
5+
samples/client_tokens.json

README.md

Lines changed: 56 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# bexio API PHP Client
1+
# Bexio API PHP Client
22

33
The bexio API Client Library enables you to work with the bexio API.
44
This is an early version and is still in development.
@@ -12,7 +12,7 @@ You can use **Composer** or download the library.
1212
Require this package with composer:
1313

1414
```sh
15-
composer require christianruhstaller/bexio-api-php-client
15+
$ composer require onlime/bexio-api-client
1616
```
1717
Include the autoloader:
1818

@@ -21,77 +21,69 @@ require_once '/path/to/your-project/vendor/autoload.php';
2121
```
2222

2323
## Examples
24-
Get access token
24+
25+
> A fully working example can be found in `samples/` directory.
26+
27+
Authenticate to get access and refresh tokens:
28+
2529
```php
30+
<?php
2631
require_once '../vendor/autoload.php';
2732

28-
$clientId = '9999999999999.apps.bexio.com'; // The client id you have received from the bexio support
29-
$clientSecret = 'W1diwrEvHlgQMPRYdr3t6I1z5sQ='; // The client secret you have received from the bexio support
30-
$redirectUri = 'http://localhost/bexio-api-php-client.php'; // Set here your Url where this script gets called
31-
$scope = 'general'; // A whitespace-separated list of scopes (see https://docs.bexio.com/oauth/scopes/).
32-
$state = '8OTs2JTDcWDaPqV7o9aHVWqM'; // A random sequence. Should be used as a protection against CSRF-Attacks
33-
$credentialsPath = 'client_credentials.json'; // Set the path where the credentials file will be stored
34-
35-
$curl = new \Curl\Curl();
36-
37-
$client = new \Bexio\Client(
38-
[
39-
'clientId' => $clientId,
40-
'clientSecret' => $clientSecret,
41-
]
42-
);
43-
$client->setRedirectUri($redirectUri);
44-
45-
// If code is not set we need to get the authentication code
46-
if (!isset($_GET['code'])) {
47-
$redirectTo = \Bexio\Client::OAUTH2_AUTH_URL.'?'.http_build_query(
48-
[
49-
'client_id' => $clientId,
50-
'client_secret' => $clientSecret,
51-
'redirect_uri' => $redirectUri,
52-
'scope' => $scope,
53-
'state' => $state,
54-
]
55-
);
56-
57-
header('Location: '.$redirectTo);
58-
exit;
59-
} else {
60-
$accessToken = $client->fetchAccessTokenWithAuthCode($_GET['code']);
61-
file_put_contents($credentialsFile, json_encode($accessToken));
62-
exit;
63-
}
33+
use Bexio\Client;
34+
35+
/**
36+
* $clientId: The client ID you have received from Bexio developer portal (https://developer.bexio.com/).
37+
* $clientSecret: The client secret you have received from Bexio developer portal (https://developer.bexio.com/).
38+
* $redirectUrl: Set your URL where this script gets called and set it as allowed redirect URL in your app settings in Bexio developer portal (https://developer.bexio.com/).
39+
* $scopes: A list of scopes (see https://docs.bexio.com/#section/Authentication/API-Scopes).
40+
* $tokensFile: Set the path where the credentials file will be stored.
41+
*/
42+
$clientId = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';
43+
$clientSecret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
44+
$redirectUrl = 'http://bexio-api-php-client.test/auth.php';
45+
$scopes = ['openid', 'profile', 'contact_edit', 'offline_access', 'kb_invoice_edit', 'bank_payment_edit'];
46+
$tokensFile = 'client_tokens.json';
47+
48+
$client = new Client($clientId, $clientSecret, $redirectUrl);
49+
$client->authenticate($scopes);
50+
$client->persistTokens($tokensFile);
6451
```
6552

66-
Init client
53+
Init client:
54+
6755
```php
68-
require_once '../vendor/autoload.php';
69-
70-
$client = new \Bexio\Client([
71-
'clientId' => 'CLIENT_ID',
72-
'clientSecret' => 'CLIENT_SECRET',
73-
]);
74-
75-
$credentialsPath = 'PATH_TO_CREDENTIAL_FILE';
76-
77-
if (!file_exists($credentialsPath)) {
78-
throw new \Exception('Credentials file not found for OAuth: '.$credentialsPath);
79-
}
80-
81-
$accessToken = file_get_contents($credentialsPath);
82-
$client->setAccessToken($accessToken);
83-
84-
if ($client->isAccessTokenExpired()) {
85-
$client->refreshToken($client->getRefreshToken());
86-
file_put_contents($credentialsPath, $client->getAccessToken());
87-
}
56+
<?php
57+
require_once '../vendor/autoload.php';
58+
59+
use Bexio\Client;
60+
61+
$clientId = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';
62+
$clientSecret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
63+
$tokensFile = 'client_tokens.json';
64+
65+
$client = new Client($clientId, $clientSecret);
66+
$client->loadTokens($tokensFile);
8867
```
8968

90-
Get contacts
69+
Get contacts:
9170

9271
```php
93-
$bexio = new \Bexio\Resource\Contact($client);
94-
95-
$contacts = $bexio->getContacts();
72+
<?php
73+
use Bexio\Resource\Contact;
74+
75+
$bexioContact = new Contact($client);
76+
$contacts = $bexioContact->getContacts();
9677
```
9778

79+
## Authors
80+
81+
Author of this awesome package is [Philip Iezzi (Onlime GmbH)](https://www.onlime.ch/).
82+
83+
Large parts of this package were ported from the original [christianruhstaller/bexio-api-php-client](https://github.com/christianruhstaller/bexio-api-php-client). Credits go to [Christian Ruhstaller](https://github.com/christianruhstaller), the bolloon guy.
84+
85+
This fork was detached from the original repository in March 2022, as the codebases have diverged quite a bit. Beware: Both projects currently don't represent a full-featured Bexio API client library, both being limited to just a subset of all API methods.
86+
87+
## License
88+
89+
This package is licenced under the [MIT license](LICENSE) however support is more than welcome.

composer.json

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
11
{
2-
"name": "christianruhstaller/bexio-api-php-client",
3-
"description": "Client library for bexio API",
2+
"name": "onlime/bexio-api-client",
3+
"description": "Client library for Bexio API",
4+
"keywords": [
5+
"onlime",
6+
"bexio-api-client",
7+
"bexio-api-php-client",
8+
"bexio"
9+
],
410
"type": "library",
511
"license": "MIT",
612
"authors": [
13+
{
14+
"name": "Philip Iezzi",
15+
"email": "philip@onlime.ch"
16+
},
717
{
818
"name": "Christian Ruhstaller",
919
"email": "christian.ruhstaller@goldinteractive.ch"
1020
}
1121
],
1222
"require": {
13-
"curl/curl": "^1.6"
23+
"php": "^8.0",
24+
"ext-json": "*",
25+
"jumbojett/openid-connect-php": "^0.9.5",
26+
"guzzlehttp/guzzle": "^7.4"
1427
},
1528
"autoload": {
1629
"psr-4": {

samples/auth.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
require_once '../vendor/autoload.php';
3+
4+
use Bexio\Client;
5+
6+
/**
7+
* $clientId: The client ID you have received from Bexio developer portal (https://developer.bexio.com/).
8+
* $clientSecret: The client secret you have received from Bexio developer portal (https://developer.bexio.com/).
9+
* $redirectUrl: Set your URL where this script gets called and set it as allowed redirect URL in your app settings in Bexio developer portal (https://developer.bexio.com/).
10+
* $scopes: A list of scopes (see https://docs.bexio.com/#section/Authentication/API-Scopes).
11+
* $tokensFile: Set the path where the credentials file will be stored.
12+
*/
13+
$clientId = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';
14+
$clientSecret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
15+
$redirectUrl = 'http://bexio-api-php-client.test/auth.php';
16+
$scopes = ['openid', 'profile', 'contact_edit', 'offline_access', 'kb_invoice_edit', 'bank_payment_edit'];
17+
$tokensFile = 'client_tokens.json';
18+
19+
$client = new Client($clientId, $clientSecret, $redirectUrl);
20+
$client->authenticate($scopes);
21+
$client->persistTokens($tokensFile);
22+
?>
23+
Sucessfully authenticated. <a href="sample.php">Proceed to sample.php</a>

samples/client_credentials.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

samples/sample.php

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,22 @@
11
<?php
2-
3-
namespace Samples;
4-
5-
use Curl\Curl;
6-
72
require_once '../vendor/autoload.php';
83

9-
//$client = new \Bexio\Client([
10-
// 'clientId' => '6313765359.apps.bexio.com',
11-
// 'clientSecret' => 'QGITKza+ftg5QYm2nL6pTE7OOT4=',
12-
//]);
13-
//
14-
//// Load previously authorized credentials from a file.
15-
//$credentialsPath = 'client_credentials.json';
16-
//
17-
//if (file_exists($credentialsPath)) {
18-
// $accessToken = file_get_contents($credentialsPath);
19-
// $client->setAccessToken($accessToken);
20-
//
21-
// if ($client->isAccessTokenExpired()) {
22-
// $client->refreshToken($client->getRefreshToken());
23-
// file_put_contents($credentialsPath, $client->getAccessToken());
24-
// }
25-
//
26-
// var_dump($client->get('contact', []));
27-
//} else {
28-
// throw new \Exception('No credentials found');
29-
//}
30-
//
31-
//
32-
//die();
33-
4+
use Bexio\Client;
5+
use Bexio\Resource\Contact;
346

7+
/**
8+
* $clientId: The client ID you have received from Bexio developer portal (https://developer.bexio.com/).
9+
* $clientSecret: The client secret you have received from Bexio developer portal (https://developer.bexio.com/).
10+
* $tokensFile: Set the path where the credentials file was stored in auth.php.
11+
*/
12+
$clientId = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';
13+
$clientSecret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
14+
$tokensFile = 'client_tokens.json';
3515

36-
$curl = new Curl();
16+
$client = new Client($clientId, $clientSecret);
17+
$client->loadTokens($tokensFile);
3718

38-
$curl->post(
39-
'https://office.bexio.com/oauth/access_token',
40-
[
41-
'client_id' => '6313765359.apps.bexio.com',
42-
'client_secret' => 'QGITKza+ftg5QYm2nL6pTE7OOT4=',
43-
'redirect_uri' => 'http://testlink.ch/_bexio/test.php',
44-
'code' => $_GET['code'],
45-
]
46-
);
19+
$bexioContact = new Contact($client);
20+
$contacts = $bexioContact->getContacts();
4721

48-
echo $curl->response;
22+
print_r($contacts);

samples/tom.php

Lines changed: 0 additions & 44 deletions
This file was deleted.

src/Bexio/AbstractClient.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
namespace Bexio;
3+
4+
abstract class AbstractClient
5+
{
6+
const METHOD_GET = 'GET';
7+
const METHOD_POST = 'POST';
8+
const METHOD_PUT = 'PUT';
9+
const METHOD_DELETE = 'DELETE';
10+
const METHOD_PATCH = 'PATCH';
11+
12+
abstract protected function request(string $path = '', string $method = self::METHOD_GET, array $data = [], array $queryParams = []);
13+
14+
public function get(string $path, array $data = [], array $queryParams = [])
15+
{
16+
return $this->request($path, self::METHOD_GET, $data, $queryParams);
17+
}
18+
19+
public function post(string $path, array $data = [], array $queryParams = [])
20+
{
21+
return $this->request($path, self::METHOD_POST, $data, $queryParams);
22+
}
23+
24+
public function put(string $path, array $data = [], array $queryParams = [])
25+
{
26+
return $this->request($path, self::METHOD_PUT, $data, $queryParams);
27+
}
28+
29+
public function delete(string $path, array $data = [], array $queryParams = [])
30+
{
31+
return $this->request($path, self::METHOD_DELETE, $data, $queryParams);
32+
}
33+
34+
public function patch(string $path, array $data = [], array $queryParams = [])
35+
{
36+
return $this->request($path, self::METHOD_PATCH, $data, $queryParams);
37+
}
38+
}

0 commit comments

Comments
 (0)