AI Development Guide - PHP SDK
This guide provides a comprehensive, fast reference for AI systems to quickly develop applications using the BosBase PHP SDK. All examples are production-ready and follow best practices.
Table of Contents
- Authentication
- Initialize Collections
- Define Collection Fields
- Add Data to Collections
- Modify Collection Data
- Delete Data from Collections
- Query Collection Contents
- Add and Delete Fields from Collections
- Query Collection Field Information
- Upload Files
- Query Logs
Authentication
Initialize Client
<?php
require_once 'vendor/autoload.php';
use BosBase\BosBase;
$pb = new BosBase('http://localhost:8090');
Password Authentication
// Authenticate with email/username and password
$authData = $pb->collection('users')->authWithPassword(
'user@example.com',
'password123'
);
// Auth data is automatically stored
echo $pb->authStore->isValid() ? 'true' : 'false'; // true
echo $pb->authStore->getToken(); // JWT token
print_r($pb->authStore->getRecord()); // User record
Check Authentication Status
if ($pb->authStore->isValid()) {
echo 'Authenticated as: ' . $pb->authStore->getRecord()['email'] . "\n";
} else {
echo "Not authenticated\n";
}
Logout
$pb->authStore->clear();
Initialize Collections
Create Base Collection
$collection = $pb->collections->createBase('articles', [
'fields' => [
[
'name' => 'title',
'type' => 'text',
'required' => true,
],
],
]);
Create Auth Collection
$collection = $pb->collections->createAuth('users', [
'fields' => [
[
'name' => 'name',
'type' => 'text',
],
],
]);
Add Data to Collections
$record = $pb->collection('articles')->create([
'title' => 'My Article',
'content' => 'Article content',
]);
Modify Collection Data
$record = $pb->collection('articles')->update('RECORD_ID', [
'title' => 'Updated Title',
]);
Delete Data from Collections
$pb->collection('articles')->delete('RECORD_ID');
Query Collection Contents
// List records
$result = $pb->collection('articles')->getList(1, 20, [
'filter' => 'status = "published"',
'sort' => '-created',
]);
// Get single record
$record = $pb->collection('articles')->getOne('RECORD_ID');
Query Collection Field Information
// Get schema for a collection
$schema = $pb->collections->getSchema('articles');
print_r($schema['fields']);
// Get all collection schemas
$result = $pb->collections->getAllSchemas();
print_r($result['collections']);
Upload Files
$record = $pb->collection('articles')->create([
'title' => 'Article with Image',
'image' => new CURLFile('/path/to/image.jpg', 'image/jpeg', 'image.jpg'),
]);
Query Logs
// Get logs (requires superuser)
$logs = $pb->logs->getList(1, 50, 'data.status >= 400');
print_r($logs['items']);
Related Documentation
- Authentication - Detailed authentication guide
- Collections - Collection management
- API Records - Record operations