How to Send Images via Laravel Guzzle HTTP client

How to Send Images via Laravel Guzzle HTTP client

public function uploadImage(Request $request) { $request->validate([ 'image' => 'required|image|mimes:jpeg,png,jpg,gif|max:2048', ]); $image = $request->file('image'); $client = new Client(); try { $response = $client->post('https://example.com/api/upload', [ 'multipart' => [ [ 'name' => 'image', 'contents' => fopen($image->getRealPath(), 'r'), 'filename' => $image->getClientOriginalName(), ], [ 'name' => 'user_id', 'contents' => 123, ], ], ]); $responseData = json_decode($response->getBody(), true); return response()->json([ 'message' => 'Image uploaded successfully!', 'data' => $responseData, ], 200); } catch (GuzzleException $e) { return response()->json([ 'message' => 'Failed to upload image.', 'error' => $e->getMessage(), ], 500); } }
Tags
Laravel Guzzle Laravel Guzzle Image Upload Send Images Using Laravel Guzzle File Upload With Laravel Guzzle Laravel Guzzle Http Client File Upload Image Transfer Via Laravel Api Laravel Guzzle File Handling Guzzle Http Client For File Uploads Learn Laravel Guzzle Image Upload Build Image Upload Feature With Laravel Guzzle Implement File Uploads With Laravel Guzzle