9951 explained code solutions for 126 technologies


php-guzzleHow to post a JSON body in PHP Guzzle?


Posting a JSON body in PHP Guzzle is easy. You can use the json option in the request options array to set the body of the request.

$client = new GuzzleHttp\Client();
$response = $client->request('POST', 'http://example.com/api/v1/users', [
    'json' => [
        'name' => 'John Doe',
        'email' => '[email protected]'
    ]
]);

The output of the above code will be a GuzzleHttp\Psr7\Response object.

Code explanation

  1. $client = new GuzzleHttp\Client(); - This creates a new Guzzle client.
  2. $response = $client->request('POST', 'http://example.com/api/v1/users', [ - This sends a POST request to the specified URL.
  3. 'json' => [ - This sets the body of the request to a JSON object.
  4. 'name' => 'John Doe', - This sets the name field of the JSON object.
  5. 'email' => '[email protected]' - This sets the email field of the JSON object.

Helpful links

Edit this code on GitHub