It seems like the issue you're encountering is related to how you're sending the parameters with the Laravel Http facade. When using the Http facade, you don't need to manually encode the parameters as JSON or use the form_params key like you do with Guzzle. Instead, you should pass the parameters directly to the post method.
Here's how you can modify your Http facade request to work similarly to your Guzzle post request:
public function makeRequest($entity, $action, $params = [])
{
$params = [
'select' => ['id'],
'limit' => 2,
];
// ...
/**
* When using the Http facade, pass the parameters directly.
* The Http facade will automatically convert the array to a query string
* for a form request, or to JSON if you use the ->asJson() method.
*/
$response = Http::withHeaders([
'X-Civi-Auth' => 'Bearer ' . $this->apiKey,
'Accept' => 'application/json', // Ensure we're accepting JSON
])->post($this->endpoint . 'LocationType/get', [
'json' => $params, // Use 'json' key to indicate JSON payload
]);
// Decode the JSON response
$locationTypes = $response->json();
dd($locationTypes);
}
In this example, I've used the json key to indicate that the parameters should be sent as a JSON payload. The Accept header is set to application/json to indicate that we expect a JSON response.
If the API expects the parameters to be sent as form parameters, you can simply pass the array to the post method without the json key:
$response = Http::withHeaders([
'X-Civi-Auth' => 'Bearer ' . $this->apiKey,
])->post($this->endpoint . 'LocationType/get', $params);
In this case, the Http facade will send the request with a Content-Type of application/x-www-form-urlencoded, and the parameters will be encoded as a URL-encoded query string.
Make sure to adjust the endpoint URL and headers according to the API's expectations. If the API requires a GET request with query parameters, you would use the get method instead of post and pass the parameters as a query string.
Remember to check the API documentation for CiviCRM to ensure you're sending the request in the format that the API expects.