Skip to content

Instantly share code, notes, and snippets.

@juampynr
Last active May 1, 2023 19:52
Show Gist options
  • Save juampynr/bfd5e8e38424618b3065b3f6a9713e69 to your computer and use it in GitHub Desktop.
Save juampynr/bfd5e8e38424618b3065b3f6a9713e69 to your computer and use it in GitHub Desktop.
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));
@ziasultan2
Copy link

$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

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