PHP code example of elytica / compute-client

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

    

elytica / compute-client example snippets


use Elytica\ComputeClient\ComputeService;

$compute = app(ComputeService::class);
echo $compute->getUserName();

use Elytica\ComputeClient\ComputeService;

$compute = new ComputeService(
    token: 'your-api-token',
    base_url: 'https://service.elytica.com',
);

$compute->getUserId();         // int
$compute->getUserName();       // string
$compute->whoami();            // raw user object
$compute->getApplications();   // available compute applications

$projects = $compute->getProjects();

$project = $compute->createNewProject(
    project_name:        'My project',
    project_description: 'Optimization run for X',
    application:         $applicationId,
    webhook_url:         'https://example.com/webhooks/compute', // optional
    webhook_secret:      'shared-secret',                         // optional
);

$compute->updateProject(
    project_id:   $project->id,
    description:  'Updated description',
);

$compute->deleteProject($project->id);

$job = $compute->createNewJob($project->id, 'Job 1', priority: 100);

// Upload an input file and assign it to the job
$upload = $compute->uploadInputFile(
    filename:   'model.lp',
    contents:   file_get_contents('/path/to/model.lp'),
    project_id: $project->id,
);

$compute->assignFileToJob(
    project_id: $project->id,
    job_id:     $job->id,
    file_id:    $upload[0]->id,
    arg:        1,
);

// Queue the job for execution
$compute->queueJob($job->id);

// Halt a running job
$compute->haltJob($job->id);

$inputs  = $compute->getInputFiles($project->id);
$outputs = $compute->getOutputFiles($job->id, $project->id);

// Stream a file to disk
$compute->downloadFile($project->id, $fileId, '/tmp/result.csv');

$context = $compute->getUserContext();

$statuses = $compute->getJobBatchStatus([101, 102, 103]);

$compute->haltJobs([101, 102, 103]);

$workflow = $compute->createProjectWorkflow(
    projectName:        'Daily run',
    projectDescription: 'Scheduled batch',
    application:        $applicationId,
    jobs: [
        ['name' => 'Job A', 'priority' => 100],
        ['name' => 'Job B', 'priority' => 50],
    ],
    webhookUrl:    'https://example.com/webhooks/compute',
    webhookSecret: 'shared-secret',
);

use Elytica\ComputeClient\ComputeService;

$raw       = file_get_contents('php://input');
$signature = $_SERVER['HTTP_X_SIGNATURE'] ?? '';

if (! ComputeService::verifyWebhookSignature($raw, $signature, $secret)) {
    http_response_code(401);
    exit;
}

$payload = json_decode($raw);
// ... handle event

$result = $compute->queueJob($job->id, function (\Throwable $e) {
    report($e);
});

if ($result === null) {
    // request failed; callback already received the exception
}

use Elytica\ComputeClient\WebsocketClient;

$ws = new WebsocketClient(
    auth_url: 'https://service.elytica.com/broadcasting/auth',
    ws_url:   'wss://ws.elytica.com:443',
    app_key:  $appKey,
    app_id:   $appId,
    token:    $token,
    timeout:  10,
);

$ws->addInitChannel("private-job.$jobId");

$ws->connect(function ($message, $conn) use ($ws) {
    // handle event
    if ($message->event === 'job.completed') {
        $ws->stop();
    }
});
bash
php artisan vendor:publish --tag=compute-config