PHP code example of viewmend / sdk

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

    

viewmend / sdk example snippets




declare(strict_types=1);

use ViewMend\ViewMend;

alse || trim($token) === '') {
    throw new \RuntimeException('VIEWMEND_API_TOKEN is    throw new \RuntimeException('VIEWMEND_INTEGRATION_ID is ',
        title: 'Homepage deployed',
    )
    ->send();

$result = $viewmend
    ->siteTracker($integrationId)
    ->events()
    ->deployment(
        id: 'deploy-abc123',
        title: 'Homepage deployed',
    )
    ->site('https://example.com')
    ->page('https://example.com/')
    ->page('https://example.com/pricing')
    ->environment('production')
    ->description('Published release abc123.')
    ->reference('https://github.com/example/project/actions/runs/123')
    ->send();

$tracker = $viewmend->siteTracker($integrationId);
$dashboard = $tracker->dashboard(device: 'desktop');

$healthScore = $dashboard->summary->healthScore; // null until data is available
$attentionItems = $dashboard->needsAttention->items;

if ($dashboard->latestCheck !== null) {
    $resources = $tracker->resources(
        runId: $dashboard->latestCheck->runId,
        type: 'images',
        device: $dashboard->scope->device,
        perPage: 50,
    );

    foreach ($resources->items as $resource) {
        // Use $resource->url, $resource->transferredBytes, and $resource->durationMs.
    }
}

use ViewMend\ViewMend;

$viewmend = ViewMend::client(token: $token);
$cron = $viewmend->cron();

$registration = $cron->register(
    cron: '*/15 * * * *',
    timezone: 'Europe/London',
    endpointPath: '/cron',
);

use ViewMend\Exception\AuthenticationException;
use ViewMend\Exception\EndpointDisabledException;
use ViewMend\Exception\NetworkException;
use ViewMend\Exception\ServerException;
use ViewMend\Exception\TokenScopeException;
use ViewMend\ViewMend;

$cron = ViewMend::client(token: $token)->cron();

try {
    $settings = $cron->current();

    if ($settings === null) {
        // No schedule has been registered. Show the initial settings form.
    } else {
        $cronExpression = $settings->cron;
        $timezone = $settings->timezone;
        $enabled = $settings->enabled;

        // Display server state such as $settings->status and $settings->nextRunAt read-only.
    }
} catch (TokenScopeException) {
    // A Site Tracker token was pasted into the Cron settings.
} catch (AuthenticationException) {
    // The Cron connection token is invalid or has been rotated.
} catch (EndpointDisabledException) {
    // The ViewMend connection is disabled.
} catch (NetworkException|ServerException) {
    // ViewMend is temporarily unavailable. Show a clearly marked cached snapshot if one exists.
}

$callback = $cron->verifyCallback($requestHeaders, $rawRequestBody);

if ($callback->isVerification()) {
    $responseBody = $callback->verificationResponseBody();
    // Return $responseBody as application/json with a 2xx status.
} else {
    // Deduplicate by $callback->runId, then run the scheduled task.
    // Return any 2xx response when processing succeeds.
}

printf(
    "Delivery %s: event %s is %s (%s)\n",
    $result->deliveryId->value,
    $result->eventId->value,
    $result->duplicate ? 'a duplicate' : 'accepted',
    $result->queueStatus->value,
);