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

lat4732's avatar
Level 12

Can someone convert this curl request into Laravel's Http facade code?

Hey!

Can someone convert this

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => $URL,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'GET',
    CURLOPT_POSTFIELDS =>'{}',
    CURLOPT_HTTPHEADER => array(
    'Content-Type: application/json'
    ),
));

$response = curl_exec($curl);

curl_close($curl);

Into Laravel Http facade code. I tried something like

$response = Http::withHeaders(['Content-Type' => 'application/json'])->post($URL);
$responseBody = json_decode($response->getBody(), true);

but dd($responseBody) gave me null. Anyways, can someone do it, please? I appreciate it.

0 likes
4 replies
click's avatar

I would start with doing the same type of request, your curl is doing a GET request while your HTTP is doing a post.

lat4732's avatar
Level 12

@click You're right. My bad. Anyways, still getting null.

$response = Http::withHeaders(['Content-Type' => 'application/json'])->get($URL);
$responseBody = json_decode($response->getBody(), true);

The endpoint I'm hitting has limited requests. That's why I'm asking for conversion from someone that knows how to set it up so I don't waste any of these requests while testing and experimenting.

click's avatar
click
Best Answer
Level 35

@Laralex so instead if debugging yourself and "waste" time of your own you "waste" time of other people, ok...

What does $response->getBody(), you can also use $response->body() give you without doing json_decode?

lat4732's avatar
Level 12

@click Okay, you're kinda rude. $response->body() is returning what I expect. Thanks.

Please or to participate in this conversation.