christianruhstaller / bexio-api-php-client
bexio API客户端库
0.3.0
2020-11-02 15:03 UTC
Requires
- curl/curl: ^1.6
This package is not auto-updated.
Last update: 2024-09-17 09:14:10 UTC
README
bexio API客户端库使您能够使用bexio API。这是一个早期版本,仍在开发中。
有关如何使用API的更多信息,请参阅bexio API文档。
安装
您可以使用Composer或下载库。
使用Composer要求此包
composer require christianruhstaller/bexio-api-php-client
包含自动加载器
require_once '/path/to/your-project/vendor/autoload.php';
示例
获取访问令牌
require_once '../vendor/autoload.php'; $clientId = 'CLIENT_ID'; // The client id of your app $clientSecret = 'CLIENT_SECRET'; // The client secret of your app $redirectUri = 'https:///bexio-api-php-client.php'; // Set here your Url where this script gets called $scope = 'openid offline_access'; // A whitespace-separated list of scopes (see https://docs.bexio.com/#section/Authentication/API-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 $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( [ 'response_type' => 'code', 'client_id' => $clientId, '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; }
初始化客户端
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()); }
获取联系人
$bexio = new \Bexio\Resource\Contact($client); $contacts = $bexio->getContacts();