在laravel中使用guzzle http将文件从一个系统发送到另一个系统

0 人关注

我正在创建一个逻辑,当我在a系统数据库的产品表中上传图片时,我想同时将图片发送到b系统的产品表数据库中。我已经测试了postman中的api存储功能,我得到了200ok状态代码,但数据没有被保存在系统b数据库的特定表中。 guzzle code to push image 我已经追踪了流程,这里的guzzle代码的所有内容似乎都在正常工作,我也对响应进行了死记硬背,得到了这个结果 $response diedumped 这是我在系统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 b on saving
       $client = new \GuzzleHttp\Client();
       $url = "http://systemb/api/push_h2image";
       $response = $client->request('POST',$url,[
           'multipart' => [
                   'Content-type' => 'multipart/form-data',
                   'name'     => $filename,
                   'contents' => file_get_contents( $dest)
    } 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;

这里是系统b中的api函数,图片和缩略图应该被接收并存储到系统b的数据库中。

    public function savesystemAimage(Request $request)
    $productdetails=new Product;
         if ($request->hasfile('file')) {
              $file = $request->file('file');
                   $extension = $file->getClientOriginalExtension(); 
                   $filename = ('p' . sprintf("%010d", $productdetails->id)) . '-m' . rand(1000, 9999) . '.' . $extension;
                   $product_id = $productdetails->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;
      $productdetails->photo_1 = $filename;
    $productdetails->thumbnail_1 = 'thumb_' . $filename;
    $productdetails->save();