php-guzzleHow to post data in PHP Guzzle?
Posting data in PHP Guzzle is easy and straightforward. The following example code block shows how to post data using Guzzle:
$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 posted data:
{
"args": {},
"data": "",
"files": {},
"form": {
"field_name": "abc",
"nested_field": {
"nested": "hello"
},
"other_field": "123"
},
"headers": {
"Accept": "*/*",
"Content-Length": "60",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "GuzzleHttp/6.3.3 curl/7.54.0 PHP/7.2.10"
},
"json": null,
"origin": "203.0.113.2",
"url": "http://httpbin.org/post"
}
The code consists of the following parts:
-
$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 specifies the form parameters to be sent with the request. -
'field_name' => 'abc',
- This is an example of a form parameter. -
]);
- This closes the form parameters array and the request method.
For more information about posting data with Guzzle, please refer to the Guzzle documentation.
More of Php Guzzle
- How to set a user agent in PHP Guzzle?
- How to set a timeout for a request with PHP Guzzle?
- How to add an SSL certificate to a request with PHP Guzzle?
- How to handle a RequestException with PHP Guzzle?
- How to make an asynchronous send with PHP Guzzle?
- How to convert a response to an array with PHP Guzzle?
- What version of PHP is required for Guzzle?
- How to make HTTP request with PHP Guzzle?
- How to stream with PHP Guzzle?
- How to troubleshoot cURL error 60 with Guzzle in PHP?
See more codes...