Level 58
To convert the cURL command to Laravel's HTTP Client, you can use the following code:
use Illuminate\Support\Facades\Http;
if (empty(env('STABILITY_API_KEY'))) {
echo "STABILITY_API_KEY environment variable is not set";
exit(1);
}
$outputFile = './out/v1_txt2img.png';
$baseUrl = env('API_HOST', 'https://api.stability.ai');
$url = "$baseUrl/v1/generation/stable-diffusion-v1-5/text-to-image";
$response = Http::withHeaders([
'Content-Type' => 'application/json',
'Accept' => 'image/png',
'Authorization' => 'Bearer ' . env('STABILITY_API_KEY')
])->post($url, [
'text_prompts' => [
[
'text' => 'A lighthouse on a cliff'
]
],
'cfg_scale' => 7,
'clip_guidance_preset' => 'FAST_BLUE',
'height' => 512,
'width' => 512,
'samples' => 1,
'steps' => 30
]);
file_put_contents($outputFile, $response->body());
This code uses Laravel's HTTP Client to make a POST request to the specified URL with the given headers and data. The response body is then saved to the specified output file. Note that the env() function is used to retrieve the values of the environment variables.
1 like