PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, February 26, 2022

[FIXED] send file to an api using guzzle http client

 February 26, 2022     api, guzzle, laravel     No comments   

Issue

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("%010d", $product_details->id)) . '-m' . sprintf("%010d", $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


Solution

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)


Answered By - N69S
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing