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 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...