使用 guzzle http 客户端将文件发送到 api

send file to an api using guzzle http client

在 web 系统中上传图像后,我想将图像推送到另一个 web 系统 b.i 我正在使用 guzzle http 客户端在系统中推送和保存图像 b.i 已经能够保存系统 a 中的图像但是当它到达要推送并保存到系统 b 的部分时,我已设置为在上传时出现错误时显示的错误 image.here 是我将图像保存在系统 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;
}

我收到错误“上传图片时发生错误”,但该错误已保存在 systema 中,但无法在 systemb 中推送。我还不明白我的代码库哪里出了问题,但我想guzzle 上的部分没有被执行,因为数据被保存在 systema 中,但它无法被推送到 systemb.what 可能是这里的问题

您的 class 产品没有声明 getPath() 方法

file_get_contents($product_details->getPath())

更改它,使其使用您在该行上方使用的路径

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