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 parameterfoo
with the valuebar
.'baz' => 'qux'
- adds the query parameterbaz
with the valuequx
.
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 update PHP Guzzle?
- How to stream with PHP Guzzle?
- How to send multipart requests with Guzzle in PHP?
- How to set a timeout for a request with PHP Guzzle?
- How to post form data with PHP Guzzle?
- How to handle a RequestException with PHP Guzzle?
- How to make an asynchronous send with PHP Guzzle?
- What version of PHP is required for Guzzle?
See more codes...