Integrating Midjourney AI Painting API with PHP: A Step-by-Step Guide

This tutorial explains how to use PHP to connect to Midjourney's AI painting API, covering preparation, request construction, cURL transmission, response handling, and a complete example that enables developers to generate and manage AI‑created artwork.

php Courses
php Courses
php Courses
Integrating Midjourney AI Painting API with PHP: A Step-by-Step Guide

With rapid advances in artificial intelligence, AI painting has become a highlight in artistic creation. Midjourney offers a powerful painting API that developers can integrate to generate artwork. This article explains how to connect the Midjourney API using PHP, covering preparation, request creation, sending, response handling, and a complete example.

1. Preparation

Register a Midjourney account and obtain an API key.

Set up a PHP development environment (PHP 5.6+).

Familiarize with basic PHP syntax.

2. Connecting to the Midjourney API

First, create the API request with URL, payload, and headers. Example code:

<?php
$apiUrl = 'https://api.midjourney.com/v1/paint';
$apiKey = 'Your API Key';
$imageUrl = 'https://example.com/image.jpg';

$payload = json_encode([
    'image_url' => $imageUrl,
    'style' => 'picasso'
]);

$headers = [
    'Content-Type: application/json',
    'Authorization: Bearer ' . $apiKey
];

Next, send the request using cURL and decode the JSON response:

<?php
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$response = curl_exec($ch);
curl_close($ch);

$data = json_decode($response, true);

Process the response: if the status is "success", retrieve the generated image URL; otherwise handle the error.

<?php
if ($data['status'] == 'success') {
    $imageUrl = $data['result']['image_url'];
    // Save or display the artwork
} else {
    $errorMessage = $data['error']['message'];
    // Handle API failure
}

3. Full Example

The complete script combines the previous steps into a single runnable example.

<?php
$apiUrl = 'https://api.midjourney.com/v1/paint';
$apiKey = 'Your API Key';
$imageUrl = 'https://example.com/image.jpg';

$payload = json_encode([
    'image_url' => $imageUrl,
    'style' => 'picasso'
]);

$headers = [
    'Content-Type: application/json',
    'Authorization: Bearer ' . $apiKey
];

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$response = curl_exec($ch);
curl_close($ch);

$data = json_decode($response, true);

if ($data['status'] == 'success') {
    $imageUrl = $data['result']['image_url'];
    // Save or display the artwork
} else {
    $errorMessage = $data['error']['message'];
    // Handle API failure
}

The guide demonstrates how to use PHP to integrate the Midjourney API, allowing developers to generate AI‑created artworks, customize styles, and explore new creative possibilities.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendArtificial IntelligencePHPMidjourneyapi-integrationAI painting
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.