php-guzzleHow to use form data with PHP Guzzle?
Using PHP Guzzle to work with form data is quite simple.
$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 data to be sent.'field_name' => 'abc',- This is an example of a field name and value pair.'nested_field' => [- This is an example of a nested field.
Helpful links
More of Php Guzzle
- How to set a user agent in PHP Guzzle?
- How to use Monolog with Guzzle in PHP?
- How to install PHP Guzzle without Composer?
- How to mock requests with Guzzle in PHP?
- How to add a bearer token in PHP Guzzle?
- How to make HTTP2 request with PHP Guzzle?
- How to stream with PHP Guzzle?
- How to set a timeout for a request with PHP Guzzle?
- How to use PHP Guzzle to make a batch request?
- How to add an SSL certificate to a request with PHP Guzzle?
See more codes...