Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sample POST request with Guzzle
<?php
require 'vendor/autoload.php';
use GuzzleHttp\Client;
$client = new Client([
'base_uri' => 'http://example.com',
]);
$payload = file_get_contents('/my-data.xml');
$response = $client->post('the/endpoint', [
'debug' => TRUE,
'body' => $payload,
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
]
]);
$body = $response->getBody();
print_r(json_decode((string) $body));
@ethanwebmaster
Copy link

ethanwebmaster commented Sep 3, 2018

Ok, Thanks, It is working well.

@Ezep
Copy link

Ezep commented Oct 2, 2018

Hi !

"body" param is deprecated in last version of Guzzle.

You must use "form_params" option to send a 'application/x-www-form-urlencoded' request
or the "multipart" request option to send a 'multipart/form-data' request.

Related: https://stackoverflow.com/a/34411797

@tallesairan
Copy link

tallesairan commented Nov 22, 2018

Hi !

"body" param is deprecated in last version of Guzzle.

You must use "form_params" option to send a 'application/x-www-form-urlencoded' request
or the "multipart" request option to send a 'multipart/form-data' request.

Related: https://stackoverflow.com/a/34411797

thanks bro

@anggasnal
Copy link

anggasnal commented Jan 21, 2019

hi dude, i have a problem

when i send form_params in guzzle, there's no value of the form sended, how to fixing this problem....

@charro0407
Copy link

charro0407 commented Sep 25, 2019

Works!! Thank you!

@theavuthnhel
Copy link

theavuthnhel commented Nov 5, 2019

Hello here!

I have two project separated: (1.) Laravel for user Key-in Data (2.) Laravel for API
(1.) Laravel for user Key-in Data work fine in local machine send Data to (2.) but on server not send body's data.

(1.) Key-in Data Project

$client = new Client(['headers' => ['X-Client-Code' => env('KEY_CODE')]]);
$request_param = [
            'client_id'    => $request->client_id,
            'code'         => $request->code,
            'phone_number' => $request->phone_number,
            'sms_type'     => 'card'
        ];
$request_data = json_encode($request_param);
$res = $client->request(
            'POST',
            url(env('API_URL').'api/v0/user/activate-card'),
            [
                'headers' => [
                    'Accept'     => 'application/json',
                    'Authorization' => file_get_contents(storage_path('credential').'/.token')

                ],
                'body'   => $request_data
            ]
        );
return $res->getBody()->getContents();

(2.) Laravel API

$ehealth_code = $request->headers->get('x-client-code');
$data = json_decode($request->getContent(),true); // return empty

Note: from my local iMac is sending body data server fine but on Ubuntu server cannot sending body data.

@sahilofficial671
Copy link

sahilofficial671 commented Jul 17, 2020

Hello here!

I have two project separated: (1.) Laravel for user Key-in Data (2.) Laravel for API
(1.) Laravel for user Key-in Data work fine in local machine send Data to (2.) but on server not send body's data.

(1.) Key-in Data Project

$client = new Client(['headers' => ['X-Client-Code' => env('KEY_CODE')]]);
$request_param = [
            'client_id'    => $request->client_id,
            'code'         => $request->code,
            'phone_number' => $request->phone_number,
            'sms_type'     => 'card'
        ];
$request_data = json_encode($request_param);
$res = $client->request(
            'POST',
            url(env('API_URL').'api/v0/user/activate-card'),
            [
                'headers' => [
                    'Accept'     => 'application/json',
                    'Authorization' => file_get_contents(storage_path('credential').'/.token')

                ],
                'body'   => $request_data
            ]
        );
return $res->getBody()->getContents();

(2.) Laravel API

$ehealth_code = $request->headers->get('x-client-code');
$data = json_decode($request->getContent(),true); // return empty

Note: from my local iMac is sending body data server fine but on Ubuntu server cannot sending body data.

Dude you're a life saver! Thank you so much i was getting error again and again you solved by encoding form params to JSON thanksss

@ziasultan2
Copy link

ziasultan2 commented Oct 20, 2020

$client = new Client();
$data = $client->request(
'POST',
'https://api.com/v1/customers/cus_MrrW/ggg',
[
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
'Authorization' => 'Basic OWQwM'
],
'form_params' => [
'dropoff_address' => $dropOffAddress,
'dropoff_name' => $user->name,
'dropoff_phone_number' => $user->phone,
'manifest' => 'groceries delivery',
'manifest_items' => $item,
'pickup_address' => $store->address,
'pickup_name' => $store->name,
'pickup_phone_number' => $store->phone
]
]
);

I'm doing like this but form_params not passing data.

@YiffyToys
Copy link

YiffyToys commented Nov 7, 2020

Hi !

"body" param is deprecated in last version of Guzzle.

You must use "form_params" option to send a 'application/x-www-form-urlencoded' request
or the "multipart" request option to send a 'multipart/form-data' request.

Related: https://stackoverflow.com/a/34411797

What do do when you post other content?
e.g. "application/json"?
So neither a MIME-Multipart nor applicaton/form-data?

@shyammtp
Copy link

shyammtp commented Jul 7, 2021

Set the header as "content-type" : "application/x-www-form-urlencoded" to accept form_params.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment