PHP code example of marceloeatworld / runpod-serverless-php

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

    

marceloeatworld / runpod-serverless-php example snippets


use MarceloEatWorld\RunPod\RunPod;

$runpod = new RunPod('your-api-key');
$endpoint = $runpod->endpoint('your-endpoint-id');

// Async run
$result = $endpoint->run([
    'prompt' => 'A beautiful landscape'
]);

// Check status
if ($result->isInQueue()) {
    echo "Job queued with ID: " . $result->id;
}

// Get status later
$status = $endpoint->status($result->id);

if ($status->isCompleted()) {
    $output = $status->getOutput();
    echo "Job completed!";
} elseif ($status->isFailed()) {
    $error = $status->getError();
    echo "Job failed: " . $error;
}

// Status checks
$response->isCompleted();  // true if status is COMPLETED
$response->isInQueue();    // true if status is IN_QUEUE
$response->isInProgress(); // true if status is IN_PROGRESS
$response->isFailed();     // true if status is FAILED
$response->isCancelled();  // true if status is CANCELLED

// Data access
$response->id;             // Job ID
$response->status;         // Status string
$response->data;           // Complete response data

// Helper methods
$response->getOutput();        // Get job output data
$response->getError();         // Get error details
$response->getMetrics();       // Get execution metrics
$response->getExecutionTime(); // Get execution time in ms
$response->getDelayTime();     // Get delay time in ms

$result = $endpoint
    ->withWebhook('https://your-site.com/webhook')
    ->run([
        'prompt' => 'Your prompt'
    ]);

$result = $endpoint
    ->withPolicy([
        'executionTimeout' => 60000,
        'lowPriority' => false,
        'ttl' => 3600000
    ])
    ->run([
        'prompt' => 'Your prompt'
    ]);

$result = $endpoint
    ->withS3Config([
        'accessId' => 'your-access-id',
        'accessSecret' => 'your-access-secret',
        'bucketName' => 'your-bucket',
        'endpointUrl' => 'your-endpoint-url'
    ])
    ->run([
        'prompt' => 'Your prompt'
    ]);

// Run endpoints
$result = $endpoint->run();      // Async execution
$result = $endpoint->runSync();  // Sync execution

// Status and control
$status = $endpoint->status($jobId);   // Get job status
$health = $endpoint->health();         // Check endpoint health
$cancel = $endpoint->cancel($jobId);   // Cancel a job
$purge = $endpoint->purgeQueue();      // Purge endpoint queue
$stream = $endpoint->stream($jobId);   // Stream job results

'runpod' => [
    'api_key' => env('RUNPOD_API_KEY'),
],

public function register()
{
    $this->app->singleton(RunPod::class, function () {
        return new RunPod(config('services.runpod.api_key'));
    });
}

use MarceloEatWorld\RunPod\RunPod;

class AIController extends Controller
{
    public function generate(RunPod $runpod, Request $request)
    {
        $endpoint = $runpod->endpoint('your-endpoint-id');
        
        $result = $endpoint->run($request->all());
        
        return response()->json([
            'job_id' => $result->id,
            'status' => $result->status
        ]);
    }
}
bash
composer