bluerocktel/glpi-php-api-client
Composer 安装命令:
composer require bluerocktel/glpi-php-api-client
包简介
PHP Client for the GLPI API
README 文档
README
This package is a light PHP Wrapper / SDK for the GLPI Project API.
Installation
This library requires PHP >=8.2.
You can install the package via composer:
composer require bluerocktel/glpi-php-api-client
Authentication
User token
To get started, first generate an App token and a User token from your GLPI dashboard. Then, you can initiate the GlpiConnector class providing your instance URL, App token and User token :
$api = new BlueRockTEL\Glpi\GlpiConnector( apiUrl: 'https://glpi.mycompany.com/apirest.php', appToken: 'KSt7zEY3QMZDXZ5Gfyy7uxV4JoolzupiRCS4GPQQ', userToken:'muaYfo14YsK9fK2wutKPyZZW9z5JXW7edc5caRt5', );
If the connector fails to retreive a Session token from the provided credentials, a
BlueRockTEL\Glpi\Exceptions\AuthenticationExceptionwill be thrown.
You can now start using the API :
$response = $api->user()->search(); // list users var_dump( $response->failed(), // true if the request returned 4xx or 5xx code. $response->json(), // json response as an array );
Usage
To query the API, you can either call each API Endpoints requests individually, or make use of provided Resources classes which groups the requests into clusters.
Using Requests
Using single requests is pretty straightforward. You can use the call() method of the GlpiConnector class to send the desired request to the instance :
use BlueRockTEL\Glpi\Endpoints; $api = new BlueRockTEL\Glpi\GlpiConnector($apiUrl, $appToken, $userToken); $response = $api->call( new Endpoints\Tickets\SearchTicketsRequest() ); $response = $api->call( new Endpoints\Users\GetUserRequest($userId: 100) );
Using Resources
Using resources is a more convenient way to query the API. Each Resource class groups requests by specific API namespaces (User, Ticket, Profile...).
use Illuminate\Support\Collection; use BlueRockTEL\Glpi\Enums\Operator; use BlueRockTEL\Glpi\Entities\SearchCriteria; use BlueRockTEL\Glpi\Entities\Columns\TicketMap; $api = new BlueRockTEL\Glpi\GlpiConnector($apiUrl, $appToken, $userToken); $criterias = new Collection([ new SearchCriteria( field: TicketMap::entity_name, operator: Operator::CONTAINS, value: $entityId, ), new SearchCriteria( field: TicketMap::assigned_id, operator: Operator::EQUALS, value: $userId, ), ]); $response = $api->ticket()->search( isDeleted: false, // only non-deleted tickets criterias: $criterias, // set search criterias columns: TicketMap::all(), // set the display columns );
Each of those namespace resources can be accessed using the GlpiConnector instance :
$connector = new GlpiConnector(...); $connector->user(): BlueRockTEL\Glpi\Resources\UserResource $connector->profile(): BlueRockTEL\Glpi\Resources\ProfileResource $connector->ticket(): BlueRockTEL\Glpi\Resources\TicketResource ...
If needed, it is also possible to create the desired resource instance manually.
use BlueRockTEL\Glpi\GlpiConnector; use BlueRockTEL\Glpi\Resources\TicketResource; $api = new GlpiConnector(...); $resource = new TicketResource($api); // same as $api->ticket() $ticket = $resource->show($ticketId)->dtoOrFail(); // make changes to $ticket... $resource->update($ticket);
Responses
After issuing a request, the returned response is always an instance of Saloon\Http\Response class.
It provides some useful methods to check the response status and get the response data.
// Check response status $response->ok(); $response->failed(); $response->status(); $response->headers(); // Get response data $response->json(); # as an array $response->body(); # as an raw string $response->dto(); # as a Data Transfer Object $response->dtoOrFail(); # as a Data Transfer Object, throwing exception on 4-5xx status
You can learn more about responses by reading the Saloon documentation, which this client uses underneath.
Entities (DTO)
When working with APIs, dealing with a raw or JSON response can be tedious and unpredictable.
To make it easier, this Api client provides a way to transform the response data into a Data Transfer Object (DTO, later called Entities). This way, you are aware of the structure of the data you are working with, and you can access the data using object typed properties instead of untyped array keys.
$response = $api->user()->show(id: 92); /** @var \BlueRockTEL\Glpi\Entities\User */ $user = $response->dtoOrFail();
Although you can use the dto() method to transform the response data into an entity, it is recommended to use the dtoOrFail() method instead. This method will throw an exception if the response status is not 2xx, instead of returning an empty DTO.
It is still possible to access the underlying response object using the getResponse() method of the DTO :
$entity = $response->dtoOrFail(); // BlueRockTEL\Glpi\Contracts\Entity $entity->getResponse(); // Saloon\Http\Response
Learn more about working with Data tranfert objects on the Saloon documentation.
Extending the Client
You may easily extend the Client by creating your own Resources, Requests, and Entities.
Then, by extending the GlpiConnector class, add your new resources to the connector :
use BlueRockTEL\Glpi\GlpiConnector; class MyCustomConnector extends GlpiConnector { public function defaultConfig(): array { return [ 'timeout' => 30, ]; } public function customResource(): \App\Resources\CustomResource { return new \App\Resources\CustomResource($this); } } $api = new MyCustomConnector($apiUrl, $appToken, $userToken); $api->customResource()->index();
bluerocktel/glpi-php-api-client 适用场景与选型建议
bluerocktel/glpi-php-api-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 229 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 12 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 bluerocktel/glpi-php-api-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bluerocktel/glpi-php-api-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 229
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-12-27