Welcome to the PocketBase PHP SDK! This SDK simplifies the process of interacting with the PocketBase API, allowing you to manage collections, records, and authentication directly through PHP.
- Authentication: Securely connect with the PocketBase API using tokens.
- CRUD Operations: Create, read, update, and delete records in specified collections.
- Dynamic Collections: Easily interact with multiple collections.
- Token Generation: Generate tokens using username and password.
- Filtering & Pagination: Optional support for filtering and paginating results.
- User Management: Handle user verification, password resets, and email changes.
- OAuth2 Provider Management: Integrate with various OAuth2 providers.
- Customizable Requests: Tailor your request handling to fit your needs.
Before you begin, ensure you have the following:
- PHP 7.4 or higher
- cURL extension enabled in PHP
- A valid PocketBase instance URL
Follow these steps to install the PocketBase PHP SDK:
-
Clone the repository or download the source files:
git clone https://github.com/itzreqle/pocketbase-php-sdk.git
-
Install required dependencies using Composer:
composer require vlucas/phpdotenv
-
Include the necessary files in your project:
require_once 'init.php'; require_once 'auth.php'; require_once 'utils.php';
-
Create a
.env
file in your project root with the following environment variables:POCKETBASE_BASE_URL=https://your-pocketbase-instance.com POCKETBASE_COLLECTION=your_collection_name POCKETBASE_API_TOKEN=your_api_token
To start using the SDK, instantiate the PocketBase
class:
$pb = new PocketBase();
$pbAuth = new PocketBaseAuth();
$pbUtils = new PocketBaseUtils();
Authenticate users with various methods:
// Password authentication
$result = $pbAuth->authWithPassword('[email protected]', 'password123');
// OAuth2 authentication
$result = $pbAuth->authWithOAuth2Flow(['provider' => 'google']);
// Refresh authentication
$result = $pbAuth->authRefresh();
Manage your records with ease:
// Get all records
$queryParams = ['filter' => 'status=active'];
$response = $pb->getAllRecords($queryParams, 1, 20);
print_r($response);
// Get record by ID
$recordId = 'your-record-id';
$response = $pb->getRecordById($recordId);
print_r($response);
// Create a new record
$data = [
'name' => 'New Item',
'description' => 'A description of the item.'
];
$response = $pb->createRecord($data);
print_r($response);
// Update a record
$recordId = 'your-record-id';
$data = ['name' => 'Updated Item Name'];
$response = $pb->updateRecord($recordId, $data);
print_r($response);
// Delete a record
$recordId = 'your-record-id';
$response = $pb->deleteRecord($recordId);
print_r($response);
Manage user accounts seamlessly:
// Create a new user
$response = $pbUtils->createUser('username', '[email protected]', 'password', 'name', 'description');
// Update a user
$updateResponse = $pbUtils->updateUser('user_id_here', [
'name' => 'New Name',
'description' => 'Updated description'
]);
// Request email verification
$pbAuth->requestVerification('[email protected]');
// Confirm email verification
$pbAuth->confirmVerification('verification_token');
// Request password reset
$pbAuth->requestPasswordReset('[email protected]');
// Confirm password reset
$pbAuth->confirmPasswordReset('reset_token', 'new_password', 'new_password_confirm');
Manage authentication tokens easily:
// Generate a new token
$username = 'your-username';
$password = 'your-password';
$response = $pb->generateToken($username, $password);
print_r($response);
// Manually set a new token
$pb->setToken('your-new-token');
All API responses are returned as associative arrays, including the status code and decoded JSON body. For example:
[
'statusCode' => 200,
'response' => [
'id' => 'abc123',
'name' => 'Item Name',
'created' => '2023-01-01T12:00:00Z'
]
]
In case of errors, the SDK will provide an error message along with the HTTP status code.
We welcome contributions! Feel free to submit a Pull Request to enhance the SDK.
If you encounter any issues or have questions, please open an issue on the GitHub repository.
This project is licensed under the MIT License.