0

I want to upload file to another server with API in a Laravel project. I already use postman and that API works! In Laravel project I can upload file to my server but I want to upload that file to another server with API.

$api_key = $_COOKIE['api_key_supplier'];
            $pic = $request->file('file');
            try {
                $client = new Client(); //GuzzleHttp\Client
                $response = $client->post($this->sub_url . "up", [
                    'headers' => [
                        'Authorization' => "$api_key"
                    ],
                    'form_params' => [
                        "filedata" => $pic,
                        "type" => 4,
                    ]
                ]);
            $res = \GuzzleHttp\json_decode($response->getBody(), true);
            if ($response->getStatusCode() == 200)
                return response()->json(['success' => 1, 'msg' => 'success']);
        } catch (ClientException $e) {
            return $e->getResponse()->getStatusCode();
        }
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • please share your code of controller. – VIKAS KATARIYA Dec 25 '19 at 06:53
  • $api_key = $_COOKIE['api_key_supplier'];$pic = $request->file('file'); – seyyed sina Dec 25 '19 at 09:23
  • try { $client = new Client(); //GuzzleHttp\Client $response = $client->post($this->sub_url . "up", [ 'headers' => [ 'Authorization' => "$api_key" ], 'form_params' => [ "filedata" => $pic, "type" => 4, ] ]); – seyyed sina Dec 25 '19 at 09:28
  • $res = \GuzzleHttp\json_decode($response->getBody(), true); if ($response->getStatusCode() == 200) return response()->json(['success' => 1, 'msg' => 'success']); } catch (ClientException $e) { return $e->getResponse()->getStatusCode(); } – seyyed sina Dec 25 '19 at 09:28
  • Thank for your Answers I found the best help for me! [This is the best way for upload file in Laravel with API](https://stackoverflow.com/questions/38133244/upload-file-using-guzzle-6-to-api-endpoint) – seyyed sina Dec 26 '19 at 09:29

0 Answers0