Skip to content

Instantly share code, notes, and snippets.

@beunwa
Last active May 27, 2024 15:05
Show Gist options
  • Save beunwa/a49233f7588441aa77da49e00f2d3d5a to your computer and use it in GitHub Desktop.
Save beunwa/a49233f7588441aa77da49e00f2d3d5a to your computer and use it in GitHub Desktop.
midjourney php generator
function generateMidjourneyImage($prompt){
$jobIdFile = md5($prompt);
if (file_exists('./jobs/' . $jobIdFile)) {
$jobId = file_get_contents('./jobs/' . $jobIdFile);
die('https://cdn.midjourney.com/' . $jobId . '/0_1.png');//ou 0_2.png ou 0_3.png ou 0_4.png
}
$client = new GuzzleHttp\Client();
$headers = [
'channelId' => $_ENV['channel_id'],
'User-Agent' => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:125.0) Gecko/20100101 Firefox/125.0',
'Accept' => '*/*',
'Accept-Language' => 'fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3',
'Accept-Encoding' => 'gzip, deflate, br',
'Referer' => 'https://www.midjourney.com/imagine',
'Content-Type' => 'application/json',
'X-CSRF-Protection' => '1',
'Origin' => 'https://www.midjourney.com',
'Alt-Used' => 'www.midjourney.com',
'Connection' => 'keep-alive',
'Cookie' => '__Host-Midjourney.AuthUserToken=' . $_ENV['MIDAUTHUSERTOKEN'] . '; __Host-Midjourney.AuthUserToken.sig=' . $_ENV['MIDAUTHUSERSIGTOKEN'] . '',
'Sec-Fetch-Dest' => 'empty',
'Sec-Fetch-Mode' => 'cors',
'Sec-Fetch-Site' => 'same-origin',
'TE' => 'trailers'
];
$body = json_encode([
'prompt' => $prompt,
'prompts' => [$prompt],
'parameters' => new stdClass(),
'flags' => [
'mode' => 'fast',
'private' => false
],
'jobType' => 'imagine',
'id' => null,
'index' => null,
'metadata' => [
'imagePrompts' => 0,
'imageReferences' => 0,
'characterReferences' => 0,
'autoPrompt' => false
]
]);
$response = $client->request('POST', 'https://www.midjourney.com/api/app/submit-jobs', [
'headers' => $headers,
'body' => $body
]);
$res = $response->getBody();
if($res){
$res = json_decode($res, true);
$job_id = $res['success'][0]['job_id'];
file_put_contents('./jobs/' . md5($prompt), $job_id);
die('https://cdn.midjourney.com/' . $job_id . '/0_1.png');
}
die('Error');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment