Отправка файлов в API - PullRequest
0 голосов
/ 08 мая 2019

Я публикую изображение в своем API, используя "guzzlehttp/guzzle": "6.3". Когда я публикую изображение в моем API, я получаю false, когда проверяю файл с помощью hasFile(). Может быть, файл не передается в мой API?

Контроллер

$client = new Client();
$url = 'http://localhost:9000/api';
$path = 'app/public/images/';
$name = '94.jpeg';
$myBody['fileinfo'] = ['449232323023'];
$myBody['image'] = file_get_contents($path.$name);
$request = $client->post($url, ['form_params' => $myBody]);
$response = $request->getBody();

return $response;

API

if (!$request->hasFile('image')) {
    return response()->json([
        'message' => 'No file',
        'photo' => $request->hasFile('image'),
        'photo_size' => $request->file('image')->getSize()
    ]);
}

1 Ответ

0 голосов
/ 08 мая 2019

Вам нужно будет добавить свой form_params в массив multipart:

// untested code

$client = new Client();

$endpoint = 'http://localhost:9000/api';
$filename = '94.jpeg';
$image = public_path('images/' . $filename);

$request = $client->post($endpoint, [
    'multipart' => [
        [
            'name' => 'fileinfo',
            'contents' => '449232323023',
        ],
        [
            'name' => 'file',
            'contents' => fopen($image, 'r'),
        ],
    ],
]);

$response = $request->getBody();

return $response;
...