PHP code example of bluerocktel / glpi-php-api-client
1. Go to this page and download the library: Download bluerocktel/glpi-php-api-client library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
bluerocktel / glpi-php-api-client example snippets
$api = new BlueRockTEL\Glpi\GlpiConnector(
apiUrl: 'https://glpi.mycompany.com/apirest.php',
appToken: 'KSt7zEY3QMZDXZ5Gfyy7uxV4JoolzupiRCS4GPQQ',
userToken:'muaYfo14YsK9fK2wutKPyZZW9z5JXW7edc5caRt5',
);
$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
);
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)
);
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
);
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);
// 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
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();
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.