Search code examples
phprequesthttpclientguzzle

PHP GuzzleHttp. How to make a post request with params?


How to make a post request with GuzzleHttp( version 5.0 ).

I am trying to do the following:

$client = new \GuzzleHttp\Client();
$client->post(
    'http://www.example.com/user/create',
    array(
        'email' => '[email protected]',
        'name' => 'Test user',
        'password' => 'testpassword'
    )
);

But I am getting the error:

PHP Fatal error: Uncaught exception 'InvalidArgumentException' with the message 'No method can handle the email config key'


Solution

  • This method is now deprecated in 6.0. Instead of body use form_params

    Try this

    $client = new \GuzzleHttp\Client();
    $client->post(
        'http://www.example.com/user/create',
        array(
            'body' => array(
                'email' => '[email protected]',
                'name' => 'Test user',
                'password' => 'testpassword'
            )
        )
    );