php - Sending file to API with http client

one text

Solution:

If you want to send the only file, then try to change your code to

'body' => fopen('C:\\Users\\username\\Desktop\\api\\' . $fileName, 'r')

If you want to send the file with some fields, you can try something like this

use Symfony\Component\Mime\Part\DataPart;
use Symfony\Component\Mime\Part\Multipart\FormDataPart;

$data = [
    'data' => 'some data',
    'file' => DataPart::fromPath('/path/to/file'),
];

$formData = new FormDataPart($data);
$headers = $formData->getPreparedHeaders()->toArray();
$headers['Authorization'] = $accessToken;
$headers['Content-Type'] = 'multipart/form-data';
$client->request('POST', 'api_url', [
    'headers' => $headers,
    'body' => $formData->bodyToIterable(),
]);

Source