gentics / mesh-php-client
Gentics Mesh PHP 客户端库
0.20.3
2023-11-14 22:51 UTC
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: 2024-09-18 03:05:26 UTC
README
PHP 客户端实现 Gentics Mesh REST API。
安装
composer require gentics/mesh-php-client
基本用法
use Gentics\Mesh\Client\MeshClient; require_once __DIR__ . '/vendor/autoload.php'; $client = new MeshClient("https://: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();
认证
$client = new MeshClient("https://: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 示例
$client = new MeshClient("https://: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 示例
使用 webroot,您可以获取任何包含 schema 中 segment 字段的节点。WebRoot 端点还能直接返回二进制数据。
$client = new MeshClient("https://: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 示例
$client = new MeshClient("https://: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();
错误处理
默认情况下,如果请求失败(例如,4xx、5xx 错误代码),将抛出异常。
try { $uuid = "5725992507e748a1a5992507e7f8a115"; $client->deleteUser($uuid)->send(); } catch (ClientException $e) { // Error could indicate a 404 $response = $e->getResponse(); echo $response->getBody()->getContents(); }
配置
可以配置底层的 Guzzle 客户端
// Change the default error behaviour to not throw error exceptions $config = ['http_errors' => false]; $client = new MeshClient("https://:8080/api/v1", $config);
兼容性
- Gentics Mesh 1.8.x
- PHP 8.1+
待办事项
- 实现完整上传支持
- 实现 eventbus websocket 支持
发布
maintenance-0.10.x 分支 版本:0.10.x - PHP 7.x, Guzzle 6
master 分支 版本:0.20.x - PHP 8.1+, Guzzle 7