kontenbase / sdk
Kontenbase SDK for PHP
dev-main
2022-06-02 11:02 UTC
Requires
- guzzlehttp/guzzle: ^7.4
Requires (Dev)
- phpunit/phpunit: ^9
This package is auto-updated.
Last update: 2024-09-07 07:25:29 UTC
README
这是Kontenbase API的官方PHP客户端。
Kontenbase是一个无代码后端API平台或后端即服务(BaaS),允许您尽可能快地创建后端API。我们使开发人员能够构建后端API,而无需触摸后端代码。
访问 Kontenbase 了解更多信息,并查看我们的文档以获取更多技术细节。
API文档
有关API文档,请访问 Kontenbase API参考。
安装
要安装SDK,您可以使用Composer或直接下载源代码并将其提取到您的目录中。如果您正在使用Composer,请运行以下命令
composer require kontenbase/sdk
用法
在开始之前,您需要在 Kontenbase仪表板 上注册以获取您项目的 API密钥。
创建客户端
<?php use Kontenbase\KontenbaseClient; $kontenbase = new KontenbaseClient([ 'apiKey': 'YOUR_API_KEY' // Your API Key from app.kontenbase.com ]);
身份验证
注册
$res = $kontenbase->auth->register([ 'firstName' => 'Ega', 'lastName' => 'Radiegtya', 'email' => 'user@mail.com', 'password' => 'password' ]); echo $res['user']; // The data of registered user echo $res['token']; // The token of registered user
登录
$res = $kontenbase->auth->login([ 'email' => 'user@mail.com', 'password' => 'password' ]); echo $res['user']; // The data of logged-in user echo $res['token']; // The token of logged-in user
获取用户信息
$res = $kontenbase->auth->user(); echo $res['user'];
更新用户信息
$res = $kontenbase->auth->update([ 'firstName' => 'Ega' ]); echo $res['user'];
注销
$res = $kontenbase->auth->logout();
数据库
创建新记录
$res = $kontenbase->service('posts')->create([ 'name' => 'Post 1', 'notes' => 'Hello Kontenbase' ]); echo $res['data'] // The data of the new record
通过ID获取记录
$res = $kontenbase->service('posts')->getById('605a251d7b8678bf6811k3b1'); echo $res['data'] // The data of the record
查找记录
获取所有记录
$res = $kontenbase->service('posts')->find(); echo $res['data'] // The data of all records
查找记录并修改结果
// limit $res = $kontenbase->service('posts')->find([ 'limit' => 10 ]);
// sort // ascending: 1 // descending: -1 $res = $kontenbase->service('posts')->find([ 'sort' => ['name' => 1] ]);
// skip $res = $kontenbase->service('posts')->find([ 'skip' => 10 ]);
// select $res = $kontenbase->service('posts')->find([ 'select' => ['name', 'notes'] ]);
// lookup all fields $res = $kontenbase->service('posts')->find([ 'lookup' => '*' ]); // lookup spesific fields $res = $kontenbase->service('posts')->find([ 'lookup' => ['categories'] ]); // lookup but only show ids $res = $kontenbase->service('posts')->find([ 'lookup' => ['_id' => '*'] ]);
根据条件查找记录
// equals $res = $kontenbase->service('posts')->find([ 'where' => [ 'name' => 'Ega' ] ]);
// does not equal $res = $kontenbase->service('posts')->find([ 'where' => [ 'name' => ['$ne' => 'Ega'] ] ]);
// contains $res = $kontenbase->service('posts')->find([ 'where' => [ 'notes' => ['$contains' => 'hello'] ] ]);
// does not contain $res = $kontenbase->service('posts')->find([ 'where' => [ 'notes' => ['$notContains' => 'hello'] ] ]);
// matches any of $res = $kontenbase->service('posts')->find([ 'where' => [ 'name' => ['$in' => ['Ega', 'Radiegtya']] ] ]);
// does not match any of $res = $kontenbase->service('posts')->find([ 'where' => [ 'name' => ['$nin' => ['Ega', 'Radiegtya']] ] ]);
// less than $res = $kontenbase->service('posts')->find([ 'where' => [ 'total' => ['$lt' => 10] ] ]);
// less than or equals $res = $kontenbase->service('posts')->find([ 'where' => [ 'total' => ['$lte' => 10] ] ]);
// more than $res = $kontenbase->service('posts')->find([ 'where' => [ 'total' => ['$gt' => 10] ] ]);
// more than or equals $res = $kontenbase->service('posts')->find([ 'where' => [ 'total' => ['$gte' => 10] ] ]);
更新记录
$res = $kontenbase->service('posts')->updateById('605a251d7b8678bf6811k3b1', [ 'notes' => 'Hello world' ]);
删除记录
$res = $kontenbase->service('posts')->deleteById('605a251d7b8678bf6811k3b1');
将记录链接以创建关系
$res = $kontenbase->service('posts')->link('605a251d7b8678bf6811k3b1', [ 'categories' => '61d26e8e2adb12b85c33029c' ]);
从其关系中取消链接记录
$res = $kontenbase->service('posts')->unlink('605a251d7b8678bf6811k3b1', [ 'categories' => '61d26e8e2adb12b85c33029c' ]);
计算总记录数
计算所有记录
$res = $kontenbase->service('posts')->count();
计算符合给定条件的记录
$res = $kontenbase->service('posts')->find([ 'where' => [ 'name' => 'Ega' ] ]);
存储
上传文件
$file = fopen('/path/to/file', 'r'); $res = $kontenbase->storage->upload($file); echo $res['data'] // The data of the uploaded file
反馈
请使用我们的 GitHub Issues 进行高级反馈。您还可以加入我们的 Discord服务器。