PHP code example of zeevx / php-brimble-sandbox

1. Go to this page and download the library: Download zeevx/php-brimble-sandbox 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/ */

    

zeevx / php-brimble-sandbox example snippets


use Zeevx\BrimbleSandbox\Sandbox;
use Zeevx\BrimbleSandbox\Requests\ExecInput;
use Zeevx\BrimbleSandbox\Requests\CreateSandbox;

$client = new Sandbox(apiKey: '...');   // or set BRIMBLE_SANDBOX_KEY

$sb = $client->sandboxes()->create(new CreateSandbox(template: 'node-22'));

echo $sb->exec(new ExecInput('node -v'))->stdout;   // "v22.x.x"

$sb->putFile('tmp/app.js', "console.log('hi')");
echo $sb->exec(new ExecInput('node /tmp/app.js'))->stdout;

$sb->destroy();

use Zeevx\BrimbleSandbox\Requests\CodeInput;
use Zeevx\BrimbleSandbox\Enums\CodeLanguage;

// Shell command (pipes, &&, redirects all work)
$result = $sb->exec(new ExecInput(
    cmd: 'npm install && npm test',
    timeoutSeconds: 120,
    cwd: '/app',
    env: ['NODE_ENV' => 'test'],
));
$result->stdout;   // string
$result->exitCode; // int
$result->succeeded(); // bool

// Code snippet, no shell escaping needed
$sb->code(new CodeInput(CodeLanguage::Python, "print('hello')"));

$stream = $sb->execStream(new ExecInput('npm install'));

foreach ($stream as $frame) {
    if ($frame->isStdout()) {
        echo $frame->data;
    } elseif ($frame->isStderr()) {
        fwrite(STDERR, $frame->data);
    }
}

$result = $stream->result();   // ExecResult with the captured output + exit code

$sb->putFile('tmp/notes.txt', 'hello');      // upload raw bytes
$contents = $sb->getFile('tmp/notes.txt');   // download as string
$stream   = $sb->downloadStream('big.bin');  // PSR-7 stream for large files

use Zeevx\BrimbleSandbox\Enums\SandboxStatus;
use Zeevx\BrimbleSandbox\Requests\UpdateEgress;
use Zeevx\BrimbleSandbox\Enums\SandboxEgressMode;

$sb->pause();                 // -> ack message
$sb->resume();
$sb->wait(SandboxStatus::Ready);   // client-side poll after a resume

$sb->updateEgress(new UpdateEgress(SandboxEgressMode::Restricted, ['api.openai.com']));

$stats = $sb->stats(hoursAgo: 6);  // CPU / memory / network

use Zeevx\BrimbleSandbox\Requests\CreateVolume;
use Zeevx\BrimbleSandbox\Enums\VolumeType;

// Snapshots
$snap = $sb->createSnapshot('nightly');
$sb->snapshots();                 // this sandbox, paginated
$client->snapshots()->list();     // all snapshots for the account
$client->snapshots()->delete($snap->id);

// Volumes
$volume = $client->volumes()->create(new CreateVolume(
    name: 'node-cache',
    sizeGB: 10,
    region: $regionId,
    type: VolumeType::Sandbox,
));
$client->volumes()->list();
$client->volumes()->delete($volume->id);

$client->regions();     // list<Region>: sandbox-eligible regions
$client->templates();   // list<Template>: available images

$page = $client->sandboxes()->list(page: 1, limit: 15);

foreach ($page as $sandbox) { /* SandboxData */ }

$page->totalCount;
$page->currentPage;
$page->hasMorePages();

use Zeevx\BrimbleSandbox\Exceptions\NotFoundException;
use Zeevx\BrimbleSandbox\Exceptions\SandboxApiException;

try {
    $client->sandboxes()->get($id);
} catch (NotFoundException $e) {
    // ...
} catch (SandboxApiException $e) {
    error_log("{$e->method} {$e->endpoint} -> {$e->status}: {$e->getMessage()}");
}

$client->sandboxes()->create($input, idempotencyKey: 'my-unique-key');

$client = new Sandbox(apiKey: '...', timeout: 90.0, maxRetries: 2);

use Zeevx\BrimbleSandbox\Config;

$client = Sandbox::fromConfig(new Config(apiKey: '...'), $myGuzzleClient);
bash
composer