Skip to content

kontenbase/kontenbase-php

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Kontenbase

This is the Official PHP client for Kontenbase API.

Kontenbase is a no-code backend API platform or Backend as a Service (BaaS) that allows you to create a backend API as fast as possible. We make it easy for developers to build backend API without having to touch backend code at all.

Visit Kontenbase for more information and see our documentation for more technical details.

API Documentation

For API documentation, visit Kontenbase API Reference.

Installation

To install the SDK you can use Composer or directly download the source and extract to your directory. If you’re using Composer, run this following command:

composer require kontenbase/sdk

Usage

Before you begin, you need to sign up on Kontenbase Dashboard to retrieve an API key of your project.

Create a client

<?php

use Kontenbase\KontenbaseClient;

$kontenbase = new KontenbaseClient([
	'apiKey': 'YOUR_API_KEY' // Your API Key from app.kontenbase.com
]);

Authentication

Register

$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

Login

$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

Get user info

$res = $kontenbase->auth->user();

echo $res['user'];

Update user info

$res = $kontenbase->auth->update([
	'firstName' => 'Ega'
]);

echo $res['user'];

Logout

$res = $kontenbase->auth->logout();

Database

Create a new record

$res = $kontenbase->service('posts')->create([
	'name' => 'Post 1',
	'notes' => 'Hello Kontenbase'
]);

echo $res['data'] // The data of the new record

Get a record by ID

$res = $kontenbase->service('posts')->getById('605a251d7b8678bf6811k3b1');

echo $res['data'] // The data of the record

Find records

Get all records

$res = $kontenbase->service('posts')->find();

echo $res['data'] // The data of all records

Find records and modify the result

// 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' => '*']
]);

Find records with criteria

// 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]
	]
]);

Update record

$res = $kontenbase->service('posts')->updateById('605a251d7b8678bf6811k3b1', [
	'notes' => 'Hello world'
]);

Delete record

$res = $kontenbase->service('posts')->deleteById('605a251d7b8678bf6811k3b1');

Link a record to create relation

$res = $kontenbase->service('posts')->link('605a251d7b8678bf6811k3b1', [
	'categories' => '61d26e8e2adb12b85c33029c'
]);

Unlink a record from its relation

$res = $kontenbase->service('posts')->unlink('605a251d7b8678bf6811k3b1', [
	'categories' => '61d26e8e2adb12b85c33029c'
]);

Count total records

Count all records

$res = $kontenbase->service('posts')->count();

Count records that match given criteria

$res = $kontenbase->service('posts')->find([
	'where' => [
		'name' => 'Ega'
	]
]);

Storage

Upload a file

$file = fopen('/path/to/file', 'r');
$res = $kontenbase->storage->upload($file);

echo $res['data'] // The data of the uploaded file

Feedback

Please use our GitHub Issues for high-level feedback. Also you can join our Discord server.

Releases

No releases published

Sponsor this project

Packages

No packages published

Languages