PHP code example of livck / cloud-php

1. Go to this page and download the library: Download livck/cloud-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/ */

    

livck / cloud-php example snippets


use LIVCK\Cloud\Builders\ServiceBuilder;
use LIVCK\Cloud\CloudClient;
use LIVCK\Cloud\Query\ServiceQuery;

$client = new CloudClient($token); // lvk_…, from your secret store

// One tag per end customer; ensure() finds or creates it.
$tag = $client->tags()->ensure('customer', '4711')->tag;

$client->services()->create(
    ServiceBuilder::http('Shop', 'https://shop.example.com')->interval(60)->tags($tag),
);

foreach ($client->services()->each(ServiceQuery::make()->withTag($tag)) as $service) {
    echo $service->name, ': ', $service->effectiveStatus->value, PHP_EOL;
}

use LIVCK\Cloud\Builders\Conditions\HttpCondition;

$catalog = $client->checkTypes(); // what the server accepts today; fetch once, reuse

$service = $client->services()->create(
    ServiceBuilder::http('Checkout', 'https://shop.example.com/health')
        ->interval(30)
        ->probes('ffm', 'hel')
        ->header('X-Api-Key', $apiKey)
        ->tags($tag, 'checkout', 'env=prod')
        ->condition(
            HttpCondition::statusCode()->gte(500)->down(),
            HttpCondition::json('status')->neq('ok')->degraded(),
            HttpCondition::responseTimeMs()->gt(2000)->degraded(),
        ),
    $catalog, // optional: validate against it before anything is sent
);

use LIVCK\Cloud\Payloads\UpdateService;

$service = $client->services()->get($id);

$client->services()->update($id, UpdateService::basedOn($service)
    ->withHeader('X-Api-Key', $newKey) // the other headers stay as stored
    ->withIntervalSeconds(60));

// Tags are replaced as a whole: send the full set, by id or by name.
$tags = [...$service->tagIds(), 'customer:4712'];
$client->services()->update($id, UpdateService::make()->withTags(...$tags));

use LIVCK\Cloud\Builders\ComponentBuilder;
use LIVCK\Cloud\Payloads\CreateStatuspage;

// Look up first, create when missing: a later run finds what an earlier one made.
$page = $client->statuspages()->findBySlug('acme-4711')
    ?? $client->statuspages()->create(CreateStatuspage::make('Status')->withSlug('acme-4711'));

// Holds every service tagged customer:4711, now and later.
$client->statuspages()->components($page->id)->create(ComponentBuilder::syncedGroup('Services', $tag));

// The customer's own hostname: attach it, then hand them the CNAME and TXT record.
$domain = $client->statuspages()->customDomains($page->id)->attach('status.customer.example');

foreach ($domain->dnsRecords() as $record) {
    echo $record->type, ' ', $record->name, ' -> ', $record->value, PHP_EOL;
}

use LIVCK\Cloud\Enums\CheckResultStatus;
use LIVCK\Cloud\Query\CheckQuery;
use LIVCK\Cloud\Query\IncidentQuery;

// Every failed check of the last day, newest first; one row per check and location.
$failed = CheckQuery::make()->withStatuses(CheckResultStatus::Down)->withFrom(new DateTimeImmutable('-1 day'));

foreach ($client->services()->eachCheck($service->id, $failed) as $check) {
    echo $check->checkedAt->format('c'), ' ', $check->probe, ' ', $check->errorMessage, PHP_EOL;
}

// Open incidents across a customer's services, at most 100 ids per request.
foreach (array_chunk($services, IncidentQuery::MAX_SERVICE_IDS) as $chunk) {
    foreach ($client->incidents()->each(IncidentQuery::make()->withServiceIds($chunk)->withResolved(false)) as $incident) {
        echo $incident->title, PHP_EOL;
    }
}

$page = $client->services()->list(ServiceQuery::make()->withPerPage(100));
echo $page->total, ' services on ', $page->lastPage, ' pages', PHP_EOL;

foreach ($client->services()->each() as $service) {
    // all of them, one page in memory at a time
}

use LIVCK\Cloud\Exceptions\ValidationException;

try {
    $client->services()->create($builder);
} catch (ValidationException $e) {
    $message = $e->firstError('settings.interval_seconds') ?? $e->errorMessage();
}

$page = $client->statuspages()->create(CreateStatuspage::make('Status'), idempotencyKey: 'order-4711-statuspage');
$service = $client->services()->create($builder, idempotencyKey: 'order-4711-shop');

use LIVCK\Cloud\Builders\Conditions\Condition;

$type = $service->checkType->isUnrecognized() ? $service->raw['check_type'] : $service->checkType->value;

ServiceBuilder::http('Shop', 'https://shop.example.com')
    ->config('new_option', true)      // a key of settings.config
    ->setting('new_setting', 3)       // a key of settings
    ->attribute('new_field', 'value') // a top-level key of the request body
    ->condition(Condition::custom('metadata.new_field', 'eq', 'expected'));

$onCall = $client->request('GET', 'oncall/on-call')->json(); // not wrapped yet

use LIVCK\Cloud\Testing\MockResponse;
use LIVCK\Cloud\Testing\RecordedRequest;

[$client, $http] = CloudClient::fake([
    MockResponse::json(['data' => $tagPayload], 201),
    MockResponse::error('Plan limit reached.', 403, extra: ['upsell' => ['reason' => 'limit', 'key' => 'services']]),
]);

// Run your code against $client, then:
$http->assertSentCount(2);
$http->assertSent(fn(RecordedRequest $r): bool => $r->matches('POST', '/v1/tags/ensure'));
bash
composer 
bash
LIVCK_CLOUD_TOKEN=lvk_… php examples/01-onboard-customer.php 4711 https://www.example.com