Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ufodisko's avatar

cURL to Laravel HTTP

Hello,

I have a cURL command that I want to convert to Laravel's HTTP Client.

I tried https://laravelshift.com/convert-curl-to-http but I got an error: The "-f" option does not exist.

What is the best way to do this?

This is the cURL command

if [ -z "$STABILITY_API_KEY" ]; then
    echo "STABILITY_API_KEY environment variable is not set"
    exit 1
fi

OUTPUT_FILE=./out/v1_txt2img.png
BASE_URL=${API_HOST:-https://api.stability.ai}
URL="$BASE_URL/v1/generation/stable-diffusion-v1-5/text-to-image"

curl -f -sS -X POST "$URL" \
  -H 'Content-Type: application/json' \
  -H 'Accept: image/png' \
  -H "Authorization: Bearer $STABILITY_API_KEY" \
  --data-raw '{
    "text_prompts": [
      {
        "text": "A lighthouse on a cliff"
      }
    ],
    "cfg_scale": 7,
    "clip_guidance_preset": "FAST_BLUE",
    "height": 512,
    "width": 512,
    "samples": 1,
    "steps": 30
  }' \
  -o "$OUTPUT_FILE"
0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
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

Please or to participate in this conversation.