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;
// 1. Create the client with your API key
$runpod = new RunPod('your-api-key');
// 2. Target a specific endpoint
$endpoint = $runpod->endpoint('your-endpoint-id');
// 3. Submit a job
$result = $endpoint->run(['prompt' => 'A beautiful landscape']);
echo "Job submitted: " . $result->id; // e.g. "cb68890e-436f-4234-..."
echo "Status: " . $result->status; // "IN_QUEUE"
$result = $endpoint->run(['prompt' => 'Write a story']);
// Wait a bit for the worker to start producing chunks
sleep(5);
$stream = $endpoint->stream($result->id);
$chunks = $stream->getStream(); // Array of stream chunks
foreach ($chunks as $chunk) {
echo $chunk['output'];
}
$result = $endpoint->run(['prompt' => 'Something expensive']);
// Changed my mind
$cancelled = $endpoint->cancel($result->id);
$status = $endpoint->status($jobId);
if ($status->isFailed() || $status->isTimedOut()) {
$retry = $endpoint->retry($jobId);
echo "Retrying: " . $retry->id; // Same job ID
echo "Status: " . $retry->status; // "IN_QUEUE"
}
$result = $endpoint
->withWebhook('https://your-site.com/api/runpod/callback')
->run(['prompt' => 'Your prompt']);
// No need to poll - RunPod will call your webhook
echo "Job submitted: " . $result->id;
$result = $endpoint
->withPolicy([
'executionTimeout' => 900000, // 15 min - max active runtime (ms)
'lowPriority' => false, // true = won't trigger worker autoscaling
'ttl' => 3600000, // 1 hour - total job lifespan from submission (ms)
])
->run(['prompt' => 'Your prompt']);
use MarceloEatWorld\RunPod\RunPod;
public function register(): void
{
$this->app->singleton(RunPod::class, function () {
return new RunPod(config('services.runpod.api_key'));
});
}
use MarceloEatWorld\RunPod\RunPod;
use Illuminate\Http\Request;
class AIController extends Controller
{
public function generate(RunPod $runpod, Request $request)
{
$endpoint = $runpod->endpoint('your-endpoint-id');
$result = $endpoint->run($request->validated());
return response()->json([
'job_id' => $result->id,
'status' => $result->status,
]);
}
public function status(RunPod $runpod, string $jobId)
{
$endpoint = $runpod->endpoint('your-endpoint-id');
$status = $endpoint->status($jobId);
return response()->json($status); // Uses JsonSerializable
}
}
use MarceloEatWorld\RunPod\RunPod;
class ProcessAITask implements ShouldQueue
{
public function __construct(
private string $endpointId,
private array $input,
) {}
public function handle(RunPod $runpod): void
{
$endpoint = $runpod->endpoint($this->endpointId);
$result = $endpoint->runSync($this->input);
if ($result->isCompleted()) {
// Store output...
}
}
}
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.