Home > Software engineering >  send file to an api using guzzle http client
send file to an api using guzzle http client

Time:02-25

after uploading an image in web system a i want to push the image to another web system b.i am using guzzle http client to push and save the image in system b.i have been able to save the image in system a but when it reaches the part to push and save to system b an error that i have set to show when there is an error on uploading the image.here is my function to save the image on system a

public function productSavePicture(Request $request)
{
    try {
        $validation = Validator::make($request->all(), [
            'product_id' => 'required',
        ]);
        if ($validation->fails()) {
            throw new \Exception("validation_error", 19);
        }
        $product_details = product::where('systemid', $request->product_id)->first();
        if (!$product_details) {
            throw new \Exception('product_not_found', 25);
        }
        if ($request->hasfile('file')) {
            $file = $request->file('file');
            $extension = $file->getClientOriginalExtension(); // getting image extension
            $company_id = Auth::user()->staff->company_id;
            if (!in_array($extension, array(
                'jpg', 'JPG', 'png', 'PNG', 'jpeg', 'JPEG', 'gif', 'GIF', 'bmp', 'BMP', 'tiff', 'TIFF'))) {
                return abort(403);
            }
            $filename = ('p' . sprintf("0d", $product_details->id)) . '-m' . sprintf("0d", $company_id) . rand(1000, 9999) . '.' . $extension;
            $product_id = $product_details->id;
            $this->check_location("/images/product/$product_id/");
            $file->move(public_path() . ("/images/product/$product_id/"), $filename);
            $this->check_location("/images/product/$product_id/thumb/");
            $thumb = new thumb();
            $dest = public_path() . "/images/product/$product_id/thumb/thumb_" . $filename;
            $thumb->createThumbnail(
                public_path() . "/images/product/$product_id/" . $filename,
                $dest,
                200);
            $systemid = $request->product_id;
            $product_details->photo_1 = $filename;
            $product_details->thumbnail_1 = 'thumb_' . $filename;
            $product_details->save();
            // push image to system on saving
            $client = new \GuzzleHttp\Client();
            $url = "http://systemb/api/push_image";
            $response = $client->request('POST',$url,[
                'headers' => [ ],
                'multipart' => [
                    [
                        'name'     => $filename,
                        'contents' => file_get_contents($product_details->getPath()),
                    ],
                ],
            ]);
        } else {
            return abort(403);
        }
    } catch (\Exception $e) {
        if ($e->getMessage() == 'validation_error') {
            return '';
        }
        if ($e->getMessage() == 'product_not_found') {
            $msg = "Error occured while uploading, Invalid product selected";
        }
        {
            $msg = "Error occured while uploading picture";
        }
        $data = view('layouts.dialog', compact('msg'));
    }
    return $data;
}

i am getting the error "Error occured while uploading picture" but the error is saved in systema but its unabe to be pushed in systemb..i havent understood where i have gone wrong with my code base but i guess that part on guzzle isnt being executed because the data is being saved in systema but its unable to be pushed to systemb.what might be the issue here

CodePudding user response:

Your class Product doesnt have the method getPath() declared

file_get_contents($product_details->getPath())

Change it so it uses the path you used above that line

file_get_contents(public_path() . "/images/product/$product_id/".$filename)
  • Related