I would start with doing the same type of request, your curl is doing a GET request while your HTTP is doing a post.
May 12, 2022
4
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.
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?
Please or to participate in this conversation.