Migrating from Sora or DALL-E? Use promo code DALLE1000 for $10 in free API credits!
Tutorials/PHP SDK
CodeCanyon / PHP2 min setup

PHP Drop-in SDK for SaaS Developers

Selling a script on CodeCanyon or building a custom PHP SaaS? Use our single-file, zero-dependency PHP class to instantly support image and video generation with multi-model failover.

1. The Drop-in Class

Save this as CreativeAI.php. It uses native PHP cURL, requiring zero Composer dependencies.

php
<?php
/**
 * CreativeAI Drop-in PHP SDK
 * Zero dependencies, built-in failover routing.
 */
class CreativeAI {
    private $api_key;
    private $base_url = "https://api.creativeai.run/v1";

    public function __construct($api_key) {
        $this->api_key = $api_key;
    }

    private function request($endpoint, $payload) {
        $ch = curl_init($this->base_url . $endpoint);
        $headers = [
            "Authorization: Bearer " . $this->api_key,
            "Content-Type: application/json"
        ];
        
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        
        $response = curl_exec($ch);
        
        if(curl_errno($ch)){
            throw new Exception("Request Error:" . curl_error($ch));
        }
        curl_close($ch);
        return json_decode($response, true);
    }

    public function generateImage($prompt, $model = "seedream-3.0", $size = "1024x1024") {
        return $this->request("/images/generations", [
            "model" => $model,
            "prompt" => $prompt,
            "size" => $size,
            "n" => 1
        ]);
    }

    public function generateVideo($prompt, $model = "auto", $webhook_url = null) {
        $payload = [
            "model" => $model,
            "prompt" => $prompt,
            "failover" => true
        ];
        if ($webhook_url) {
            $payload['webhook_url'] = $webhook_url;
        }
        return $this->request("/video/generations", $payload);
    }
}
?>

2. Usage Example

Instantiate the client and call the methods. The auto model parameter handles fallback logic automatically to ensure your users never see downtime.

php
<?php
require 'CreativeAI.php';

$client = new CreativeAI("YOUR_API_KEY");

// 1. Generate an Image
$imageResponse = $client->generateImage("A cinematic sunset over a futuristic city");
if (isset($imageResponse['data'][0]['url'])) {
    echo "Image URL: " . $imageResponse['data'][0]['url'] . "\n";
}

// 2. Generate a Video (async)
$videoResponse = $client->generateVideo(
    "Drone shot of a coastal highway", 
    "auto", // Auto-routes to Kling or Seedance
    "https://your-app.com/webhook/video-complete"
);

echo "Video Generation Task ID: " . $videoResponse['id'] . "\n";
?>

Upgrade Your Script Today

Give your CodeCanyon buyers access to the most reliable media API. Support DALL-E 3 fallback, Kling V3, and Seedance instantly.