php-guzzleHow to post form data with PHP Guzzle?
Posting form data with PHP Guzzle is easy and straightforward.
$client = new GuzzleHttp\Client();
$response = $client->request('POST', 'http://httpbin.org/post', [
'form_params' => [
'field_name' => 'abc',
'other_field' => '123',
'nested_field' => [
'nested' => 'hello'
]
]
]);
The output of the above code will be a response object containing the response from the server.
Code explanation
$client = new GuzzleHttp\Client();
- This creates a new Guzzle client object.$response = $client->request('POST', 'http://httpbin.org/post', [
- This sends a POST request to the specified URL.'form_params' => [
- This is an array containing the form parameters to be sent with the request.'field_name' => 'abc',
- This is an example of a form parameter, in this case a field named 'field_name' with a value of 'abc'.]);
- This closes the array of form parameters and the request.
Helpful links
More of Php Guzzle
- How to use PHP Guzzle to make a batch request?
- How to set a user agent in PHP Guzzle?
- How to set a timeout for a request with PHP Guzzle?
- How to use Promises with PHP Guzzle (with an example)?
- How to send multipart requests with Guzzle in PHP?
- How to install PHP Guzzle without Composer?
- How to use cookies with Guzzle in PHP?
- How to add an authorization header bearer in PHP Guzzle?
- How to stream with PHP Guzzle?
- What version of PHP is required for Guzzle?
See more codes...