PHP code example of wavespeedai / wavespeed-php

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

    

wavespeedai / wavespeed-php example snippets


use WaveSpeed\WaveSpeed;

$output = WaveSpeed::run(
    'wavespeed-ai/z-image/turbo',
    ['prompt' => 'Cat'],
);

echo $output['outputs'][0]; // Output URL

use WaveSpeed\Client;

$client = new Client(apiKey: 'your-api-key');
$output = $client->run('wavespeed-ai/z-image/turbo', ['prompt' => 'Cat']);

use WaveSpeed\Config;

Config::set(Config::API_KEY, $_ENV['WAVESPEED_API_KEY']);

$output = $client->run(
    'wavespeed-ai/z-image/turbo',
    ['prompt' => 'Cat'],
    timeout: 300.0,          // Max wait for completion; null (default) = no local limit
    pollInterval: 1.0,       // Status check interval (default: 1.0)
    enableSyncMode: false,   // Best-effort sync result attempt (default: false)
    maxRetries: null,        // Task-level retries (default: the client's setting)
);

use WaveSpeed\Exception\SyncModeTimeoutException;

try {
    $output = $client->run(
        'wavespeed-ai/z-image/turbo',
        ['prompt' => 'Cat'],
        enableSyncMode: true,
    );
} catch (SyncModeTimeoutException $e) {
    // Not a failure — the task is still running.
    $state = $client->getResult((string) $e->taskId);
}

$result = $client->runNoThrow('wavespeed-ai/z-image/turbo', ['prompt' => 'Cat']);

// ['status' => 'completed'|'failed'|'processing',
//  'outputs' => list|null, 'task_id' => string, 'error' => string|null]
if ($result['outputs'] !== null) {
    echo $result['outputs'][0];
} else {
    error_log("{$result['status']} ({$result['task_id']}): {$result['error']}");
}

$state = $client->getResult('task-id-from-earlier');

echo $state['data']['status'];   // e.g. "processing" or "completed"
print_r($state['data']['outputs'] ?? []);

use WaveSpeed\Client;

$client = new Client(
    apiKey: 'your-api-key',
    maxRetries: 0,             // Replacement task attempts (default: 0)
    maxConnectionRetries: 5,   // Result-query GET retries; POST is never retried
    retryInterval: 1.0,        // Base delay between retries in seconds (default: 1.0)
);

use WaveSpeed\Client;
use WaveSpeed\Support\CallableLogger;
use WaveSpeed\Support\StderrLogger;

$client = new Client(logger: new StderrLogger());

// Or bridge to PSR-3 / any framework logger:
$client = new Client(
    logger: new CallableLogger(fn (string $m) => $psrLogger->warning($m)),
);

use WaveSpeed\WaveSpeed;

$url = WaveSpeed::upload('/path/to/image.png');
echo $url;

// A stream works too — including one you did not open from disk.
$url = WaveSpeed::upload($stream);

use WaveSpeed\Client;
use WaveSpeed\Http\CurlHttpClient;

$client = new Client(
    http: new CurlHttpClient([CURLOPT_PROXY => 'http://proxy.internal:3128']),
);
bash
composer 
bash
WAVESPEED_API_KEY=your-key php examples/run.php "a lighthouse at dusk"
WAVESPEED_API_KEY=your-key php examples/upload.php /path/to/image.png
WAVESPEED_API_KEY=your-key php examples/sync_mode.php
WAVESPEED_API_KEY=your-key php examples/run_no_throw.php
WAVESPEED_API_KEY=your-key php examples/configuration.php