php-guzzleHow to add query parameters to a request with PHP Guzzle?
Query parameters can be added to a request with PHP Guzzle using the query method.
Example code
$client = new GuzzleHttp\Client();
$response = $client->request('GET', 'http://example.com', [
'query' => [
'foo' => 'bar',
'baz' => 'qux'
]
]);
Output example
GuzzleHttp\Psr7\Response {#7
-reasonPhrase: "OK"
-statusCode: 200
-headers: array:7 [ …7]
-headerLines: array:7 [ …7]
-protocol: "1.1"
-stream: GuzzleHttp\Psr7\Stream {#8 …}
}
Code explanation
$client = new GuzzleHttp\Client();- creates a new Guzzle client instance.$response = $client->request('GET', 'http://example.com', [- sends a GET request to the specified URL.'query' => [- specifies the query parameters to be added to the request.'foo' => 'bar',- adds the query parameterfoowith the valuebar.'baz' => 'qux'- adds the query parameterbazwith the valuequx.
Helpful links
More of Php Guzzle
- How to keep alive with Guzzle in PHP?
- How to set a user agent in PHP Guzzle?
- How to install PHP Guzzle without Composer?
- How to set Guzzle options in PHP?
- How to make HTTP request with PHP Guzzle?
- How to make a GET request with headers with PHP Guzzle?
- How to make an asynchronous send with PHP Guzzle?
- How to use GraphQL with PHP Guzzle?
- How to use multipart/form-data with PHP Guzzle?
See more codes...