PHP code example of pocketbase / sdk-php

1. Go to this page and download the library: Download pocketbase/sdk-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pocketbase / sdk-php example snippets




use PocketBase\SDK\Client\PocketBaseClient;
use PocketBase\SDK\Query\QueryBuilder;

$pb = new PocketBaseClient('http://127.0.0.1:8090');

// Como admin (superuser)
$auth = $pb->authAsAdmin('[email protected]', 'password123');

// Como usuario de una colección
$auth = $pb->authWithPassword('users', '[email protected]', 'password');

// Verificar estado
if ($pb->isAuth()) {
    echo "Autenticado";
}

// Listar con filtros, orden y expansión
$query = (new QueryBuilder())
    ->filter('status = "active"')
    ->sortDesc('created')
    ->expand('author,category')
    ->fields('id,title,author')
    ->page(1)
    ->perPage(20);

$records = $pb->collection('posts')->getList($query);

// Obtener uno
$post = $pb->collection('posts')->getOne('record_id');

// Crear
$new = $pb->collection('posts')->create([
    'title' => 'Nuevo post',
    'content' => 'Contenido...',
]);

// Actualizar
$updated = $pb->collection('posts')->update('record_id', [
    'title' => 'Título actualizado',
]);

// Eliminar
$pb->collection('posts')->delete('record_id');

$collections = $pb->collections()->getList();
$collection = $pb->collections()->getOne('posts');

use PocketBase\SDK\Exception\ApiException;
use PocketBase\SDK\Exception\AuthException;

try {
    $pb->authAsAdmin('[email protected]', 'wrongpassword');
} catch (AuthException $e) {
    echo "Error de auth: " . $e->getMessage();
} catch (ApiException $e) {
    echo "Error API: " . $e->getMessage();
    echo "Código: " . $e->statusCode;
}

$pb = new PocketBaseClient(string $baseUrl, ?Client $httpClient = null)

(new QueryBuilder())
    ->filter(string $filter)           // Agregar filtro
    ->filterAnd(string ...$filters)    // Filtros AND
    ->filterOr(string ...$filters)     // Filtros OR
    ->sort(string $field, bool $desc)  // Ordenar
    ->sortAsc(string $field)           // Orden ascendente
    ->sortDesc(string $field)          // Orden descendente
    ->expand(string ...$relations)    // Expandir relaciones
    ->fields(string ...$fields)       // Seleccionar campos
    ->page(int $page)                  // Página
    ->perPage(int $perPage)            // Registros por página
    ->limit(int $limit)                // Alias de perPage
bash
composer