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

christogonus's avatar

Help to Convert PHP Curl into Http Guzzle Request in Laravel

I have the curl code

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://api.orange.com/oauth/v3/token',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS => 'grant_type=client_credentials',
  CURLOPT_HTTPHEADER => array(
    'Content-Type: application/x-www-form-urlencoded',
    'Authorization: {{authorization_header}}'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

Now, I have translated it to

Http::withHeaders([
                'Content-Type' => 'application/x-www-form-urlencoded',
                'Authorization' => $this->authorization_header,
            ])
            ->post(
                'https://api.orange.com/oauth/v3/token',
                [
                    'form_params' => [
                        'grant_type' => 'client_credentials'
                    ],
                ]
            )

but I get the error response {"error": "invalid_request","error_description": "Missing grant_type parameter."}

0 likes
3 replies
LaryAI's avatar
Level 58

It looks like you're trying to convert a cURL request to a Guzzle HTTP request in Laravel. The error you're encountering suggests that the grant_type parameter is not being sent correctly. In Guzzle (which Laravel's Http facade is based on), you don't need to specify 'form_params' as a key in the array when sending application/x-www-form-urlencoded requests. Instead, you should pass the form parameters directly as the second argument to the post method.

Here's how you can translate your cURL request to a Guzzle HTTP request using Laravel's Http facade:

use Illuminate\Support\Facades\Http;

$response = Http::withHeaders([
    'Content-Type' => 'application/x-www-form-urlencoded',
    'Authorization' => $this->authorization_header,
])->post('https://api.orange.com/oauth/v3/token', [
    'grant_type' => 'client_credentials',
]);

echo $response->body();

Make sure to replace $this->authorization_header with the actual authorization header value you need to use.

This should correctly send the grant_type parameter in the body of the POST request as application/x-www-form-urlencoded. The echo $response->body(); will output the response body from the request.

christogonus's avatar

Unfortunately for me, this doesnt work either.

I added the ```'form_params' `` while trying to find a solution.

gych's avatar
gych
Best Answer
Level 29

Can you try it like this

Http::withHeaders([
	'Content-Type' => 'application/x-www-form-urlencoded',
	'Authorization' => $this->authorization_header,
])
->asForm()
->post('https://api.orange.com/oauth/v3/token',[
	'grant_type' => 'client_credentials',
]);

Like its stated in the docs https://laravel.com/docs/11.x/http-client#sending-form-url-encoded-requests

If you would like to send data using the application/x-www-form-urlencoded content type, you should call the asForm method before making your request

Please or to participate in this conversation.