Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

laocoi's avatar

How to make Post Request from Controller in Laravel?

Hello all,

I have 2 server, I want when I upload image to server 1 (S1), this image will be upload to server 2 (S2). So i think i need make a POST request from Controller on S1.

For upload image to another server: i have tried some way as curl, guzzle with no success.

Please let me know how to make a POST request.

0 likes
9 replies
bobbybouwmann's avatar

Well guzzle or curl is actually the right way. I would prefer guzzle though. Can you show what you have tried?

laocoi's avatar

@bobbybouwmann Thanks for reply, this is my code:

My image on localhost: file:///C:/Users/Le/Downloads/test.jpg

use GuzzleHttp\Client;

$client = new Client();

$file = 'file:///C:/Users/Le/Downloads/test.jpg';

$this->client->request("POST", $urlonS2, array('Content-Type => multipart/form-data'),'file'=>$file);

bobbybouwmann's avatar

You can't just send a file over the internet without the proper headers and using the correct locations! You can try something like this:

$request = $client->post('/files')
    ->addPostFile('file', 'test.doc')

Note: just look at the API and see what you can find there ;)

Mithridates's avatar

@mr66 why don't you upload the image directly from your view to your second server?

laocoi's avatar

@bobbybouwmann i have tried and it still not work with error in log:

[2016-02-02 15:18:20] s3.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Call to undefined method GuzzleHttp\Psr7\Response::addPostFile()' in ...

Guzzle function i have tried:

$client = new Client();

$file = public_path($pathOfImageOnS1);

$request = $client->post('s2/upload.php')->addPostFile('file', $file);

Did the right thing your comment?

@Mithridates Because i buy this script, and it use javascript for handling post request from view to controller, after that this javascript response to view data return from controller. My JS is bad :(

bobbybouwmann's avatar

You know that Guzzle is php right?

Like I said, look at the API. If you have no clue then you should contact the guy from who you bought the script and pay him for freelance work ;)

laocoi's avatar
laocoi
OP
Best Answer
Level 1

@bobbybouwmann Thank you, i have knew what is my problem.

use Guzzle\Http\Client;

replace for use GuzzleHttp\Client;

Final Guzzle function for another:

$client = new Client();

$file = public_path($pathofimage);

$request = $client->post('/upload.php')->addPostFiles(array('file' => $file),('Content-Type: multipart/form-data'));

$response = $request->send();

Mithridates's avatar

@mr66 maybe I don't figure out your situation totally, but this approach seems to me really nasty.
The over all time of response time of your app will dramatically drain with this approach I think.
Along with some possible security issues.

pakuize's avatar

I can see it's been a while with this thread, but I agree with @Mithridates You would be better off, throwing the image onto a amazon s3 and having the other server checking for updates on s3 bucket and pulling down the new images.

Please or to participate in this conversation.