PHP code example of 1monitor / sdk-php

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

    

1monitor / sdk-php example snippets


$client = new OneMonitor\Sdk\Client();
$client->ping('your-monitor-token');

use OneMonitor\Sdk\Client;

$client = new Client();

$client->ping('tok_abc');   // "this job is alive"

use OneMonitor\Sdk\Client;

$client = new Client();
$token = 'tok_abc';

$client->pingStart($token);

try {
    doTheWork();
    $client->pingSuccess($token);
} catch (Throwable $e) {
    $client->pingFail($token, output: $e->getMessage());

    throw $e;
}

$exitCode = 0;
$stderr = '';

// ... run the job, capturing $exitCode and $stderr ...

if ($exitCode === 0) {
    $client->pingSuccess($token, exitCode: $exitCode);
} else {
    $client->pingFail($token, exitCode: $exitCode, output: $stderr);
}

$client->pingSuccess($token, version: '1.4.2');

$client = new Client(version: getenv('APP_VERSION') ?: null);

$client->pingStart($token);    // ?version=<APP_VERSION>
$client->pingSuccess($token);  // ?version=<APP_VERSION>

#!/usr/bin/env php
 // bin/nightly-report

nt();
$token = getenv('ONEMONITOR_TOKEN') ?: '';

$client->pingStart($token);

try {
    (new NightlyReport())->run();
    $client->pingSuccess($token);
} catch (Throwable $e) {
    $client->pingFail($token);
    fwrite(STDERR, $e->getMessage() . PHP_EOL);

    exit(1);
}

$client = new OneMonitor\Sdk\Client(
    timeout: 2.0,
    retries: 1,
    logger: $psrLogger,
);

$client = new OneMonitor\Sdk\Client(
    httpClient: new GuzzleHttp\Client([
        'timeout' => 5.0,
        'headers' => ['X-Deploy-Key' => 'secret'],
    ]),
);

if (!$client->pingSuccess($token)) {
    // The work is done regardless. Whether you care is up to you.
}

use OneMonitor\Sdk\ClientInterface;

final class NightlyReport
{
    public function __construct(private ClientInterface $monitor)
    {
    }
}
bash
composer 
json
"1monitor/sdk-php": "^0.5"