gentics / mesh-php-client
Gentics Mesh PHP Client Library
Installs: 13 105
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 15
Forks: 2
Open Issues: 4
Requires
- php: ^8.1
- ext-json: *
- guzzlehttp/guzzle: ^7.4.5
- ioflair/php-proxy: ^4.0.2
Requires (Dev)
- enlightn/security-checker: ^1.10.0
- friendsofphp/php-cs-fixer: ^3.8.0
- isaac/composer-git-hooks: ^2.0.0
- overtrue/phplint: ^5.2.2
- phpunit/phpunit: ^9.5.21
- squizlabs/php_codesniffer: ^3.7.1
- dev-master
- 0.20.3
- 0.20.2
- 0.20.1
- 0.20.0
- 0.10.1
- 0.10.0
- 0.8
- 0.7
- 0.6
- 0.5
- 0.4
- 0.3
- 0.2
- 0.1
- dev-dependabot/composer/laminas/laminas-diactoros-2.25.2
- dev-dependabot/composer/guzzlehttp/psr7-2.5.0
- dev-master-webrootCreate
- dev-maintenance-0.10.x
- dev-0.7-pluginRequest
- dev-master-pluginRequest
- dev-release-0.8
- dev-master-fix-file-upload
- dev-support-multipart-arrays
- dev-master-SUP-11984-fix-empty-file-post
- dev-fixLoadByUuidParams
This package is not auto-updated.
Last update: 2025-03-05 05:21:04 UTC
README
PHP Client implementation for Gentics Mesh REST API.
Installation
composer require gentics/mesh-php-client
Basic usage
use Gentics\Mesh\Client\MeshClient; require_once __DIR__ . '/vendor/autoload.php'; $client = new MeshClient("http://localhost:8080/api/v1"); // Load user info (sync) $request = $client->me(); $response = $request->send(); echo $response->getBody(); // Load user info (async) $promise = $request->sendAsync(); $promise->then(function ($response) { echo 'I completed! ' . $response->getBody(); }); $promise->wait(); // Load users and apply paging $request = $client->findUsers(["perPage" => 1]); $response = $request->send(); echo $response->getBody();
Authentication
$client = new MeshClient("http://localhost:8080/api/v1"); // You can either login and use the build-in cookie handling // Keep in mind that your cookie will not be automatically refreshed. // Any authenticated request will refresh the cookie and keep you authenticated. $client->login("admin", "admin")->wait(); // Or use a dedicated API key which will never expire // You can use the mesh-cli to generate a key // Setting the API key will invalidate any previously set login token information $client->setAPIKey("eyJ0eXAiOiJKV1QiLC … ZYYJbD8HllF6XZT0xRTxr3i4b9PY");
GraphQL Example
$client = new MeshClient("http://localhost:8080/api/v1"); $query = [ "query" => "{users{elements{uuid, username}}}" ]; $request = $client->graphQL("demo", $query); $response = $request->send(); $json = $response->toJson(); $this->assertEquals("anonymous", $json->data->users->elements[0]->username);
WebRoot Example
With webroot you can fetch any node which contains a segment
field in the schema.
The webroot endpoint is also able to directly return binary data.
$client = new MeshClient("http://localhost:8888/api/v1"); $request = $client->webroot("demo", "/images/yacht-pelorus.jpg"); $response = $request->send(); // You can check whether the webroot response returns json or otherwise binary data (e.g. image data) $response->isJson();
CRUD Example
$client = new MeshClient("http://localhost:8080/api/v1"); $client->login("admin", "admin")->wait(); // 1. Create User $request = [ "username" => "guzzle", "password" => "geheim", ]; $uuid = "5725992507e748a1a5992507e7f8a115"; $userResp = $client->createUserWithUuid($uuid, $request)->send()->toJson(); // 2. Read user $user = $client->findUserByUuid($uuid)->send()->toJson(); // 3. Update user $user->username = "hugo"; $updated = $client->updateUser($uuid, $user)->send()->toJson(); // 4. Delete user $client->deleteUser($uuid)->send();
Error Handling
By default exceptions will be thrown if the request fails (e.g. 4xx, 5xx error code).
try { $uuid = "5725992507e748a1a5992507e7f8a115"; $client->deleteUser($uuid)->send(); } catch (ClientException $e) { // Error could indicate a 404 $response = $e->getResponse(); echo $response->getBody()->getContents(); }
Configuration
It is possible to configure the underlying Guzzle client:
// Change the default error behaviour to not throw error exceptions $config = ['http_errors' => false]; $client = new MeshClient("http://localhost:8080/api/v1", $config);
Compatibility
- Gentics Mesh 1.8.x
- PHP 8.1+
TODOs
- Implement full upload support
- Implement eventbus websocket support
Release
maintenance-0.10.x branch Versioning: 0.10.x - PHP 7.x, Guzzle 6
master branch Versioning: 0.20.x - PHP 8.1+, Guzzle 7