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

This comment has been minimized.

Copy link

ethanwebmaster commented Sep 3, 2018

Ok, Thanks, It is working well.

@Ezep

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

Copy link

charro0407 commented Sep 25, 2019

Works!! Thank you!

@theavuthnhel

This comment has been minimized.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.