I have a method using gullzehttp and would like to change it to the pool plus the pool implements the Request method
<?php
use GuzzleHttp\Client;
$params = ['password' => '123456'];
$header = ['Accept' => 'application/xml'];
$options = ['query' => $params, 'headers' => $header];
$response = $client->request('GET', 'http://httpbin.org/get', $options);
I need to change to the Request method, but I could not find in the documentation how to send querystring variables in the Request
<?php
use GuzzleHttp\Psr7\Request;
$request = new Request('GET', 'http://httpbin.org/get', $options);
And finally, you can provide the query request option as a string. Guzzle provides several methods for uploading data. You can send requests that contain a stream of data by passing a string, resource returned from fopen, or an instance of a Psr\Http\Message\StreamInterface to the body request option.
The body can be used as a string, cast to a string, or used as a stream like object. You can provide query string parameters with a request in several ways. You can specify the query string parameters using the query request option as an array. Providing the option as an array will use PHP's http_build_query function to format the query string.
In Guzzle, you can send GET parameters to the endpoint using the ‘query’ array as shown in the above code. Usually, each HTTP response comes with a specific status code. There are several status codesand some of them are as follows. 200: Request succeeded. 201: The request succeeded and a new resource was created. 400: Bad request.
GuzzleHttp\RequestOptions::QUERY Query strings specified in the query option will overwrite all query string values supplied in the URI of a request. Float describing the timeout to use when reading a streamed body Defaults to the value of the default_socket_timeout PHP ini setting
You need to add the query as a string to the URI.
For that you can use http_build_query or a guzzle helper function to convert a parameter array to an encoded query string:
$uri = new Uri('http://httpbin.org/get');
$request = new Request('GET', $uri->withQuery(GuzzleHttp\Psr7\build_query($params)));
// OR
$request = new Request('GET', $uri->withQuery(http_build_query($params)));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With