> ## Documentation Index
> Fetch the complete documentation index at: https://docs.modelslab.com/llms.txt
> Use this file to discover all available pages before exploring further.

# PHP SDK

> Official PHP client for the ModelsLab API - generate images, videos, audio, and more.

## Overview

The ModelsLab PHP SDK provides a clean, object-oriented interface to interact with all ModelsLab APIs. It handles authentication, request formatting, and response parsing automatically.

<CardGroup cols={2}>
  <Card title="Packagist" icon="php" href="https://packagist.org/packages/modelslab/php">
    View on Packagist
  </Card>

  <Card title="GitHub" icon="github" href="https://github.com/Modelslab/modelslab-php">
    Source code and issues
  </Card>
</CardGroup>

## Installation

Install the SDK using Composer:

```bash theme={null}
composer require modelslab/php
```

Or add it to your `composer.json`:

```json theme={null}
{
    "require": {
        "modelslab/php": "^1.0.2"
    }
}
```

<Tip>
  Requires PHP 7.4 or higher and Guzzle HTTP client.
</Tip>

## Quick Start

Generate your first AI image:

```php theme={null}
<?php
require_once 'vendor/autoload.php';

use ModelsLab\ModelsLab;
use ModelsLab\Schemas\RealtimeText2ImageSchema;

$apiKey = 'your-api-key';
$modelslab = new ModelsLab($apiKey);

$image = new RealtimeText2ImageSchema([
    'prompt' => 'A majestic lion in a savanna at sunset, photorealistic, 8k',
    'negative_prompt' => 'blurry, low quality, distorted',
    'width' => 512,
    'height' => 512,
    'samples' => 1,
    'num_inference_steps' => 30,
    'guidance_scale' => 7.5
]);

$response = $modelslab->realtime()->textToImage($image);

if ($response['status'] === 'success') {
    echo "Image URL: " . $response['output'][0];
} elseif ($response['status'] === 'processing') {
    echo "Processing, request ID: " . $response['id'];
}
```

## Client Configuration

### Basic Setup

```php theme={null}
<?php
use ModelsLab\ModelsLab;

// Method 1: Direct API key
$modelslab = new ModelsLab('your-api-key');

// Method 2: With custom options
$modelslab = new ModelsLab('your-api-key', [
    'base_url' => 'https://modelslab.com/api/',
    'fetch_retry' => 10,
    'fetch_timeout' => 2
]);
```

### Environment Variables

Set your API key using environment variables:

```bash theme={null}
export MODELSLAB_API_KEY="your-actual-api-key"
# Or
export API_KEY="your-actual-api-key"
```

Then in your code:

```php theme={null}
$modelslab = new ModelsLab(getenv('MODELSLAB_API_KEY'));
```

***

## Image Generation

### Text to Image (Realtime)

```php theme={null}
<?php
use ModelsLab\ModelsLab;
use ModelsLab\Schemas\RealtimeText2ImageSchema;

$modelslab = new ModelsLab('your-api-key');

$image = new RealtimeText2ImageSchema([
    'prompt' => 'A cyberpunk city at night, neon lights, rain, cinematic',
    'negative_prompt' => 'blurry, low quality, distorted, deformed',
    'width' => 1024,
    'height' => 1024,
    'samples' => 1,
    'num_inference_steps' => 30,
    'guidance_scale' => 7.5,
    'seed' => 12345  // Optional: for reproducible results
]);

$response = $modelslab->realtime()->textToImage($image);
echo json_encode($response, JSON_PRETTY_PRINT);
```

### Image to Image

```php theme={null}
<?php
use ModelsLab\Schemas\RealtimeImage2ImageSchema;

$image = new RealtimeImage2ImageSchema([
    'init_image' => 'https://example.com/your-image.jpg',
    'prompt' => 'Transform into a watercolor painting style',
    'negative_prompt' => 'photo, realistic',
    'width' => 512,
    'height' => 512,
    'strength' => 0.7,
    'num_inference_steps' => 30,
    'guidance_scale' => 7.5
]);

$response = $modelslab->realtime()->imageToImage($image);
echo json_encode($response, JSON_PRETTY_PRINT);
```

### Community Models

```php theme={null}
<?php
use ModelsLab\Schemas\Text2Image;

$image = new Text2Image([
    'model_id' => 'flux',
    'prompt' => 'Portrait of a woman, oil painting style, renaissance',
    'negative_prompt' => 'modern, photo, blurry',
    'width' => 512,
    'height' => 768,
    'samples' => 1,
    'num_inference_steps' => 30,
    'guidance_scale' => 7.5
]);

$response = $modelslab->community()->textToImage($image);
echo json_encode($response, JSON_PRETTY_PRINT);
```

***

## Video Generation

### Text to Video

```php theme={null}
<?php
use ModelsLab\ModelsLab;
use ModelsLab\Schemas\Text2Video;

$modelslab = new ModelsLab('your-api-key');

$video = new Text2Video([
    'prompt' => 'A spaceship flying through an asteroid field, cinematic, 4K',
    'negative_prompt' => 'low quality, blurry, static',
    'model_id' => 'cogvideox',
    'width' => 512,
    'height' => 512,
    'num_frames' => 25,
    'num_inference_steps' => 20,
    'guidance_scale' => 7
]);

$response = $modelslab->video()->textToVideo($video);

// Video generation is async
if ($response['status'] === 'processing') {
    echo "Processing, request ID: " . $response['id'];
    echo "ETA: " . $response['eta'] . " seconds";
}
```

### Image to Video

```php theme={null}
<?php
use ModelsLab\Schemas\Image2Video;

$video = new Image2Video([
    'init_image' => 'https://example.com/landscape.jpg',
    'prompt' => 'Clouds moving slowly, birds flying in the distance',
    'model_id' => 'cogvideox',
    'num_frames' => 25,
    'num_inference_steps' => 20
]);

$response = $modelslab->video()->imageToVideo($video);
echo json_encode($response, JSON_PRETTY_PRINT);
```

***

## Audio Generation

### Text to Speech

```php theme={null}
<?php
use ModelsLab\ModelsLab;
use ModelsLab\Schemas\Text2Speech;

$modelslab = new ModelsLab('your-api-key');

$tts = new Text2Speech([
    'prompt' => 'Hello! Welcome to ModelsLab. This is a sample of our text-to-speech API.',
    'voice_id' => 'madison',
    'language' => 'english'
]);

$response = $modelslab->audio()->textToSpeech($tts);
echo "Audio URL: " . $response['output'][0];
```

### Music Generation

```php theme={null}
<?php
use ModelsLab\Schemas\MusicGenSchema;

$music = new MusicGenSchema([
    'prompt' => 'Upbeat electronic dance music with heavy bass drops',
    'duration' => 30
]);

$response = $modelslab->audio()->musicGen($music);
echo json_encode($response, JSON_PRETTY_PRINT);
```

### Voice to Voice (Voice Cloning)

```php theme={null}
<?php
use ModelsLab\Schemas\Voice2VoiceSchema;

$voice = new Voice2VoiceSchema([
    'init_audio' => 'https://example.com/source-speech.mp3',
    'target_audio' => 'https://example.com/voice-to-clone.mp3'
]);

$response = $modelslab->audio()->voice2Voice($voice);
echo json_encode($response, JSON_PRETTY_PRINT);
```

### Sound Effects (SFX)

```php theme={null}
<?php
use ModelsLab\Schemas\SFXSchema;

$sfx = new SFXSchema([
    'prompt' => 'Thunder rolling in the distance with heavy rain',
    'duration' => 10
]);

$response = $modelslab->audio()->sfxGen($sfx);
echo json_encode($response, JSON_PRETTY_PRINT);
```

***

## Image Editing

### Background Removal

```php theme={null}
<?php
use ModelsLab\ModelsLab;
use ModelsLab\Schemas\BackgroundRemoverSchema;

$modelslab = new ModelsLab('your-api-key');

$bgRemover = new BackgroundRemoverSchema([
    'image' => 'https://example.com/photo.jpg'
]);

$response = $modelslab->imageEditing()->backgroundRemover($bgRemover);
echo "Image without background: " . $response['output'][0];
```

### Super Resolution (Upscale)

```php theme={null}
<?php
use ModelsLab\Schemas\SuperResolutionSchema;

$upscale = new SuperResolutionSchema([
    'image' => 'https://example.com/low-res-image.jpg',
    'scale' => 4  // 2x or 4x
]);

$response = $modelslab->imageEditing()->superResolution($upscale);
echo json_encode($response, JSON_PRETTY_PRINT);
```

### Object Removal

```php theme={null}
<?php
use ModelsLab\Schemas\ObjectRemovalSchema;

$removal = new ObjectRemovalSchema([
    'image' => 'https://example.com/photo.jpg',
    'mask_image' => 'https://example.com/mask.png'
]);

$response = $modelslab->imageEditing()->objectRemover($removal);
echo json_encode($response, JSON_PRETTY_PRINT);
```

***

## 3D Model Generation

### Text to 3D

```php theme={null}
<?php
use ModelsLab\ModelsLab;
use ModelsLab\Schemas\Text23DSchema;

$modelslab = new ModelsLab('your-api-key');

$model = new Text23DSchema([
    'prompt' => 'A medieval sword with ornate handle',
    'num_inference_steps' => 50
]);

$response = $modelslab->threeD()->textTo3D($model);
echo json_encode($response, JSON_PRETTY_PRINT);
```

***

## Interior Design

### Interior Redesign

```php theme={null}
<?php
use ModelsLab\ModelsLab;
use ModelsLab\Schemas\InteriorSchema;

$modelslab = new ModelsLab('your-api-key');

$interior = new InteriorSchema([
    'init_image' => 'https://example.com/room-photo.jpg',
    'prompt' => 'Modern minimalist living room with Scandinavian furniture'
]);

$response = $modelslab->interior()->interior($interior);
echo json_encode($response, JSON_PRETTY_PRINT);
```

***

## Error Handling

The SDK throws exceptions for various error conditions:

```php theme={null}
<?php
use ModelsLab\ModelsLab;
use ModelsLab\Schemas\Text2Speech;

try {
    $modelslab = new ModelsLab('your-api-key');

    $tts = new Text2Speech([
        'prompt' => 'Hello world',
        'voice_id' => 'madison',
        'language' => 'english'
    ]);

    $response = $modelslab->audio()->textToSpeech($tts);

    if ($response['status'] === 'success') {
        echo "Audio URL: " . $response['output'][0];
    } elseif ($response['status'] === 'processing') {
        echo "Processing, check back later with ID: " . $response['id'];
    } elseif ($response['status'] === 'error') {
        echo "API Error: " . $response['message'];
    }

} catch (InvalidArgumentException $e) {
    // Invalid API key or parameters
    echo "Invalid argument: " . $e->getMessage();
} catch (RuntimeException $e) {
    // Network or API errors
    echo "Request failed: " . $e->getMessage();
}
```

***

## Async Processing Pattern

For long-running operations, poll for results:

```php theme={null}
<?php
function generateVideoWithPolling($modelslab, $prompt, $timeout = 300) {
    $video = new Text2Video([
        'prompt' => $prompt,
        'model_id' => 'cogvideox',
        'width' => 512,
        'height' => 512,
        'num_frames' => 25
    ]);

    $response = $modelslab->video()->textToVideo($video);

    if ($response['status'] === 'success') {
        return $response['output'][0];
    }

    if ($response['status'] !== 'processing') {
        throw new Exception($response['message'] ?? 'Generation failed');
    }

    $requestId = $response['id'];
    $startTime = time();

    while (time() - $startTime < $timeout) {
        sleep(5);  // Wait 5 seconds between polls

        // Use fetch endpoint to check status
        // Return URL when complete
        // Throw exception if failed
    }

    throw new Exception('Timeout waiting for video generation');
}
```

***

## Available APIs Summary

| API              | Methods                                                                        |
| ---------------- | ------------------------------------------------------------------------------ |
| **Realtime**     | `textToImage()`, `imageToImage()`                                              |
| **Community**    | `textToImage()`, `imageToImage()`, `inpainting()`, `controlnet()`              |
| **Video**        | `textToVideo()`, `imageToVideo()`                                              |
| **Audio**        | `textToSpeech()`, `musicGen()`, `voice2Voice()`, `sfxGen()`, `speechToText()`  |
| **ImageEditing** | `backgroundRemover()`, `superResolution()`, `objectRemover()`, `outpainting()` |
| **ThreeD**       | `textTo3D()`, `imageTo3D()`                                                    |
| **Interior**     | `interior()`, `roomDecorator()`, `exterior()`                                  |

***

## Next Steps

<CardGroup cols={2}>
  <Card title="API Reference" icon="book" href="/image-generation/overview">
    Explore all API endpoints
  </Card>

  <Card title="Models" icon="cube" href="https://modelslab.com/models">
    Browse available models
  </Card>

  <Card title="Webhooks" icon="bell" href="/webhooks">
    Set up async notifications
  </Card>

  <Card title="Error Codes" icon="triangle-exclamation" href="/error-codes">
    Handle errors properly
  </Card>
</CardGroup>
