如何将图像从Laravel控制器发送到API Rest [英] How to send image from Laravel controller to API Rest
本文介绍了如何将图像从Laravel控制器发送到API Rest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要从 Laravel 中的存储中获取图像,并将其从Controller发送到外部API REST.
I need to take an image from storage in Laravel and send it from a Controller to an external API REST.
我正在使用guzzlehttp multipart,但API未收到文件,返回file = null
I am using guzzlehttp multipart but the API didn't receive the file, returns file = null
推荐答案
几天前,这就是我的方法(从Request中获取文件):
This is how I did it a few days ago (taking file from Request):
对于单个文件:
public function storeProductImage(Request $request, $id){
$image = $request->file('image');
$body = [
"headers" => [
"Accept" => "multipart/form-data"
],
"multipart" => [
"name" => "image",
"contents" => file_get_contents($image),
"filename" => $image->getClientOriginalName()
]
];
return \GuzzleHttp\Client::request('POST', 'product/'.$id.'/images', $body);
}
对于多个文件:
For multiple files:
public function storeProductImage(Request $request, $id){
$body = [ "headers" => [
"Accept" => "multipart/form-data"
],
"multipart" => []
];
$images = $request->file('image');
if (is_array($images)) {
foreach ($images as $image) {
array_push($body["multipart"], ["name" => "image[]",
"contents" => file_get_contents($image),
"filename" => $image->getClientOriginalName()]);
}
}
return \GuzzleHttp\Client::request('POST', 'product/'.$id.'/images', $body);
}
这篇关于如何将图像从Laravel控制器发送到API Rest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文