bokbasen/php-api-client

Bokbasen API 的通用 PHP API 客户端

v3.0.3 2024-04-09 15:00 UTC

This package is auto-updated.

Last update: 2024-09-09 15:51:21 UTC


README

Build Status Code Coverage Scrutinizer Code Quality

如果您在寻找1.*版本的文档,请点击此处

用于所有 Bokbasen API 的通用 API 客户端,在这些 API 中没有实现特定 SDK 且需要身份验证。

HTTP 客户端使用简单,您必须自行实现 API 特定功能。但它提供了一个标准接口,用于对 Bokbasen API 进行请求,并允许您使用 Login SDK 处理与身份验证相关的任何复杂性。

首先创建一个 Login 对象 详情请见 php-sdk-auth

安装

我们使用 HttpClientDiscovery,因此您需要要求一个 PSR-7 兼容的实现,例如;

$ composer require php-http/guzzle6-adapter

然后添加我们的包

$ composer require bokbasen/php-api-client

使用方法

use Bokbasen\Auth\Login;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;

try {
    /* This example is using a file cache for the TGT, you can replace this with any PSR-6 
        compatible cache. Always using caching in production to avoid performance penalty of 
        creating and deleting tokens.
    */
    $cache = new FilesystemAdapter();
    $login = new Login('username', 'password', Login::URL_PROD, $cache);
} catch(\Throwable $e) {
    // error handling
}
use Bokbasen\ApiClient\Client;
use Bokbasen\ApiClient\HttpRequestOptions;
use Bokbasen\ApiClient\Exceptions\BokbasenApiClientException;

try {
    /* Pass the base URL of the API you are interacting with. You can also pass a logger 
        and a custom http client. Any request made through the API returns an instance 
        of \Psr\Http\Message\ResponseInterface.  All of these API calls will include the 
        necessary authentication headers.
    */
    $client = new Client($login, 'https://loan.api.boknett.no');
    
    // Execute get request, it is recommended to explicitly set accept parameter
    $headers = ['Accept' => HttpRequestOptions::CONTENT_TYPE_JSON];
    $response = $client->get('/path', $headers);
    
    // Execute POST request with json data
    $response = $client->postJson('/path', $body, $headers);
    
    // Execute POST request 
    $response = $client->post('/path', $body, $headers);
    
    // Execute PUT request
    $response = $client->put('/path', $body, $headers);
    
    // Execute PATCH request
    $response = $client->patch('/path', $body, $headers);
} catch(BokbasenApiClientException $e){
    //error handling
}

测试

$ ./vendor/bin/phpunit