Simple PHP API Client for DALL-E: Creating Images from Prompts

In the world of AI, creating art and imagery with generative models has become an exciting reality. DALL-E, developed by OpenAI, is one such groundbreaking model that can generate images from textual prompts. In this article, we'll walk you through the process of building a straightforward PHP API client to connect to DALL-E, allowing you to harness its creative potential by generating images based on your prompts. To get started, you'll need an OpenAI API key as part of your OpenAI user account.

You can also just copy the simple PHP client class below, which will give you a head start on creating a quick proof of concept for image generation.

 

Setting Up Your OpenAI Account

Before we dive into building the PHP API client for DALL-E, you'll need to make sure you have an OpenAI account and an API key. If you don't already have one, you can sign up for an OpenAI account and obtain your API key from the OpenAI Developer platform (we cover this in more detail separately).

 

Once you have your API key, you're ready to start using your PHP client.

 


class DallEClient {
    
    private static $open_ai_key = 'your_open_ai_api_key_goes_here';
    private static $open_ai_url = 'https://api.openai.com/v1';
    
    public static function generateImages($prompt, $n = 1, $size = '1024x1024', $response_format = 'url', $model='dall-e-3', $user = '') {
        //doc: https://platform.openai.com/docs/api-reference/images/create
        //create message to post
        $message = new stdClass();
        $message -> prompt = $prompt;
        $message -> n = $n;
        $message -> size = $size;
        $message -> response_format = $response_format;
        $message -> model = $model;
        if($user) {
            $message -> user = $user;
        }
        
        $result = self::_sendMessage('/images/generations', json_encode($message));
        
        return $result;
    }
    
    
    private static function _sendMessage($endpoint, $data = '', $method = 'post') {
        $apiEndpoint = self::$open_ai_url.$endpoint;
        
        $curl = curl_init();
        
        if($method == 'post') {
            $params = array(
                CURLOPT_URL => $apiEndpoint,
                CURLOPT_SSL_VERIFYHOST => false,
                CURLOPT_SSL_VERIFYPEER => false,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 90,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => "POST",
                CURLOPT_NOBODY => false,
                CURLOPT_HTTPHEADER => array(
                  "content-type: application/json",
                  "accept: application/json",
                  "authorization: Bearer ".self::$open_ai_key
                )
            );
            curl_setopt_array($curl, $params);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            
        } else if($method == 'get') {
            $params = array(
                CURLOPT_URL =>  $apiEndpoint . ($data!=''?('?'.$data):''),
                CURLOPT_SSL_VERIFYHOST => false,
                CURLOPT_SSL_VERIFYPEER => false,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 90,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => "GET",
                CURLOPT_NOBODY => false,
                CURLOPT_HTTPHEADER => array(
                  //"cache-control: no-cache",
                  "content-type: application/json",
                  "accept: application/json",
                  "authorization: Bearer ".self::$open_ai_key
                )
            );
            curl_setopt_array($curl, $params);
        }
        
        $response = curl_exec($curl);
        
        curl_close($curl);
        
        $data = json_decode($response, true);
        if(!is_array($data)) return array();
        
        return $data;
    }
}

 

Customizing Your DALL-E Client

You can further customize your DALL-E API client to meet your specific needs. 

For example, you can adjust the prompt to generate images with different concepts and scenes. Some examples of prompt modifiers that work are:

  • black and white
  • outdoors
  • neon lighting
  • monochromatic

 

You can just add the above as plain text to your prompt, and DALL-E will abide. 

For best results, the model DALL-E-3 should be used, however currently it only support at most 1 image generation at a time.

 

Conclusion

With your simple PHP API client for DALL-E, you have the power to create images from your prompts, unlocking a world of creative possibilities. As DALL-E and similar generative AI models continue to evolve, the potential for generating art, visuals, and even concepts is limitless. By connecting to the OpenAI API, you can explore the exciting world of AI-generated imagery and use it in a variety of applications, from art and design to content creation and more. 

One more important tip, is to moderate your text prompts before sending them to OpenAI - this is one step developers often forget.




The fields marked with * are required.

I have read the privacy policy.