Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Guzzle Request query params

Tags:

php

guzzle

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);
like image 984
Elvis Reis Avatar asked Mar 01 '17 17:03

Elvis Reis


People also ask

How do I send a query request in guzzle?

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.

How do I use query string parameters with a request?

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.

How to send get parameters to the endpoint in guzzle?

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.

What is the use of Query option in guzzlehttp?

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


1 Answers

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)));
like image 165
Rob Avatar answered Oct 09 '22 14:10

Rob