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

jondoe's avatar

How to use Guzzle to post data to external URL?

I am using a payment service so I need to post a form to an external URL. I am looking into Guzzle but I am confused how to use it.

<form name="form1" method="post" action="https://exampledomain.com/postpayment.php">
<input type="hidden" name="trans_type" value=" 2"/>
<input type="hidden" name="collect_total_amt" value="{{ $payment->total }}"/>

Some values are hard coded like trans_type, return_url but some are pulled from the database like total_amt, CRESecureId.

After the data is posted a reponse is sent back like this

https://example.com/return.html?order_id=6&code=000&msg=Success&error=&mP=XXXXXXXXXXXX3801&name=   Salvatore%20F%20IozziaPcTxID=ABCDEFG&sid=ddc2e76644e8dde7308d42606f7f7e74
0 likes
6 replies
jusahah87's avatar

I am not sure if I understood the question correctly. Do you need Guzzle at all here? Your end-user posts a HTML form, and that form contains generated link for the external service to post response back (eventually). Hmm.

Anyways, here is my example doing a Guzzle post request to external SMS service:

            // Do the actual SMS stuff
            // First instantiate Guzzle client
            $client = new Client([
                'timeout'  => 5.0,
            ]); 

            $smsData = [
                'username' => config('app.zoneruser'),
                'password' => config('app.zonerpw'),
                'numberto' => config('app.zonerdefaultphone'),
                'numberfrom' => '1234567',
                'message' => 'Hello there'
            ];
            
            // Call external API
            $response = $client->post(config('app.zonerurl'), ['form_params' => $smsData]);

        // Check whether API call was successfull or not...
            $zonerStatusCode = $response->getStatusCode();
            $zonerResponse = (string)$response->getBody();
pmall's avatar

Yes I think your form must directly send data to your provider.

eriktobben's avatar

If a user is not filling anything in the form, do you need it? Instead of using hidden fields (where a user easily can change the values), make a POST request from a controller method using Guzzle and define the return_url to another method on your controller and collect the data using $_GET['subject'] (assuming that your payment provider returns the data in a GET request).

jusahah87's avatar
Level 5

@eriktobben The way I understood this is that when the end-user posts the hidden HTML form he is directed to some page (on paymentservice.com) where he then fills his credit card details. In this case the form is pretty much required.

@jondoe Maybe you just need a Route matching that return URL. Something like:

routes.php


Route::post('responsefrompaymentservice', ['uses' => 'PaymentController@savePaymentDetails']);

PaymentController.php


public function savePaymentDetails(Request $request) {

    // Validate request data here somehow
    $this->validate($request->all());

    Payment::create([
        'order_id' => $request->get('order_id'),
        'success' => $request->get('success'),
        'error' => $request->get('error')
    ]);
}

No Guzzle required here.

1 like
eriktobben's avatar

@jusahah87 Ahh, I see. Then he should only need a route for fetching the results like you have done in your answer :)

inilabs's avatar

@jusahah87 i tried your code but in laravel i need to send csrf token. when i send with csrf token it does not match because its two different project.

Please or to participate in this conversation.