146

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' => 'test@gmail.com',
        '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'

Arsen
  • 3,541
  • 4
  • 14
  • 7

5 Answers5

254

Since Marco's answer is deprecated, you must use the following syntax (according jasonlfunk's comment) :

$client = new \GuzzleHttp\Client();
$response = $client->request('POST', 'http://www.example.com/user/create', [
    'form_params' => [
        'email' => 'test@gmail.com',
        'name' => 'Test user',
        'password' => 'testpassword',
    ]
]);

Request with POST files

$response = $client->request('POST', 'http://www.example.com/files/post', [
    'multipart' => [
        [
            'name'     => 'file_name',
            'contents' => fopen('/path/to/file', 'r')
        ],
        [
            'name'     => 'csv_header',
            'contents' => 'First Name, Last Name, Username',
            'filename' => 'csv_header.csv'
        ]
    ]
]);

REST verbs usage with params

// PUT
$client->put('http://www.example.com/user/4', [
    'body' => [
        'email' => 'test@gmail.com',
        'name' => 'Test user',
        'password' => 'testpassword',
    ],
    'timeout' => 5
]);

// DELETE
$client->delete('http://www.example.com/user');

Async POST data

Usefull for long server operations.

$client = new \GuzzleHttp\Client();
$promise = $client->requestAsync('POST', 'http://www.example.com/user/create', [
    'form_params' => [
        'email' => 'test@gmail.com',
        'name' => 'Test user',
        'password' => 'testpassword',
    ]
]);
$promise->then(
    function (ResponseInterface $res) {
        echo $res->getStatusCode() . "\n";
    },
    function (RequestException $e) {
        echo $e->getMessage() . "\n";
        echo $e->getRequest()->getMethod();
    }
);

Set headers

According to documentation, you can set headers :

// Set various headers on a request
$client->request('GET', '/get', [
    'headers' => [
        'User-Agent' => 'testing/1.0',
        'Accept'     => 'application/json',
        'X-Foo'      => ['Bar', 'Baz']
    ]
]);

More information for debugging

If you want more details information, you can use debug option like this :

$client = new \GuzzleHttp\Client();
$response = $client->request('POST', 'http://www.example.com/user/create', [
    'form_params' => [
        'email' => 'test@gmail.com',
        'name' => 'Test user',
        'password' => 'testpassword',
    ],
    // If you want more informations during request
    'debug' => true
]);

Documentation is more explicits about new possibilities.

Samuel Dauzon
  • 10,744
  • 13
  • 61
  • 94
  • To send query string in the post requests I've found better using 'query' option inside the params, because somehow in the url string it only took the 1st one http://docs.guzzlephp.org/en/latest/request-options.html#query – marcostvz Jun 22 '16 at 13:21
137

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' => 'test@gmail.com',
            'name' => 'Test user',
            'password' => 'testpassword'
        )
    )
);
Machavity
  • 30,841
  • 27
  • 92
  • 100
Marco
  • 1,476
  • 1
  • 11
  • 9
  • 100
    This method is now deprecated in 6.0. Instead of `body` use `form_params`. – jasonlfunk Aug 13 '15 at 21:29
  • 6
    Passing in the "body" request option as an array to send a POST request has been deprecated. Please use the "form_params" request option to send a application/x-www-form-urlencoded request, or a the "multipart" request option to send a multipart/form-data request. – Jeremy Quinton Jan 19 '16 at 16:38
54

Note in Guzzle V6.0+, another source of getting the following error may be incorrect use of JSON as an array:

Passing in the "body" request option as an array to send a POST request has been deprecated. Please use the "form_params" request option to send a application/x-www-form-urlencoded request, or a the "multipart" request option to send a multipart/form-data request.

Incorrect:

$response = $client->post('http://example.com/api', [
    'body' => [
        'name' => 'Example name',
    ]
])

Correct:

$response = $client->post('http://example.com/api', [
    'json' => [
        'name' => 'Example name',
    ]
])

Correct:

$response = $client->post('http://example.com/api', [
    'headers' => ['Content-Type' => 'application/json'],
    'body' => json_encode([
        'name' => 'Example name',
    ])
])
Scott Yang
  • 2,348
  • 2
  • 18
  • 21
2
$client = new \GuzzleHttp\Client();
$request = $client->post('http://demo.website.com/api', [
    'body' => json_encode($dataArray)
]);
$response = $request->getBody();

Add

openssl.cafile in php.ini file

ben.IT
  • 1,490
  • 2
  • 18
  • 37
Prakash D
  • 21
  • 3
0

You May Easy Call Multipart Api for image uploading directly using use GuzzleHttp\Client;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Utils;
use File;

$filename = $req->file('file1')->getClientOriginalName();
        $getfilePath  = $req->file('file1')->getRealPath();
        $client = new Client();
$response = $client->request('POST', 'http://127.0.0.1:8045/api/uploadImages', [
    'multipart' => [
        [
            'name'     => 'image',
            'contents' => fopen($getfilePath, 'r')
        ],
        // 'headers'  => [
        //      'Content-Type' => '<Content-type header>'
        //  ]
       
    ]
]);
echo $response->getStatusCode();
$bodyresponcs = $response->getBody();
$result = json_decode($bodyresponcs);
print_r($result->status);
Shahzeb Naqvi
  • 313
  • 2
  • 8