I’m trying to make a POST request using GuzzleHttp in PHP. I need to send parameters along with the request, including a request body, files, and a header with a Bearer token. Can someone provide a simple example of how to achieve this?
To make a POST request with parameters using GuzzleHttp in PHP, you can follow this simple example:
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', $api_url, [
'headers' => [
'Authorization' => "Bearer {$access_token}"
]
'form_params' => [
'username' => 'testuser',
'password' => 'testpassword',
]
]);
If you want to send file.
$response = $client->request('POST', $api_url, [
'multipart' => [
[
'name' => 'file_name',
'contents' => fopen('/path/to/file', 'r')
]
]
]);
This code will send a POST request with the specified parameters, including the request body, files, and the Bearer token header. You can get more details from this link