Skip to content

PHP SDK for CBWS Cloud platform

The Compute service SDK is part of our CBWS SDK.

Supported CBWS cloud platform services:

Installation

The recommended way to install our SDK is:

composer require cbws/sdk

Note you will need the PHP gRPC extension and the PHP protobuf extensions.

Usage

Authentication

The CBWS SDK can either authenticate using the CBWS CLI config file or a IAM service account JSON file.

By default the CBWS SDK will try to discover a valid credential in the following order:

  1. A IAM service account credentials file at the location set the CBWS_CREDENTIALS environment variable.
  2. A IAM service account credentials file stored at $HOME/.config/cbws/cbws.json
  3. The CBWS CLI config file at $HOME/.cbws.yaml

The SDK can also be configured to use a specific service account credentials file (in this example the Compute service is used):

<?php
use Cbws\Sdk\Compute\Client;
use Cbws\Sdk\Auth\Credentials;

new Client('some-project', Credentials::fromCredentialsFile('/path/to/service-account.json', []));

Long-running operations

Many interactions with the CBWS cloud platform will be long-running operations that run on the background, these can be polled until the operation has finished. After which the response or error will be available.

Getting the current operation status

Operations have a get() method that fetches the current state of the operation.

<?php
$operation = $machine->stop();

// Get the latest status of the operation
$operation = $operation->get();

var_dump($operation->getDone());
var_dump($operation->getResponse());
if ($operation->getError() !== null) {
   throw $operation->getError();
}

Async/await with PHP fibers

With the built-in PHP fibers it is possible to wait until the operation has finished, either successfully or with an error:

<?php
$operation = $machine->stop();

// Get the fiber that will wait until the operation has finished.
$fiber = $operation->fiber();
$fiber->start();

while (!$fiber->isTerminated()) {
    // Get intermediate operation state
    $operation = $fiber->resume();
}

// Finished operation
$operation = $fiber->getReturn();

var_dump($operation->getDone());
var_dump($operation->getResponse());
if ($operation->getError() !== null) {
   throw $operation->getError();
}