Sending an array through a query string in guzzle

Guzzle client creates default from this code

$client->get('https://example.com/{?a}', array('a' => array('c','d'))); 

this url

 https://example.com/?a=c,d 

What is the best practice of sending an array to a query string in a RESTful application? The question is, how can I determine on the server side whether c,d string or an array? Isn't it better to send arrays using square brackets, for example. a[]=c&a[]=d ? How can I set Guzzle to use square brackets? Or is it better to use JSON encoded variables? On the server side, I use Tonic .

+7
source share
2 answers

Working solution:

 $vars = array('state[]' => array('Assigned','New'), 'per_page' => $perPage, 'page' => $pageNumber); $query = http_build_query($vars, null, '&'); $string = preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '=', $query); // state[]=Assigned&state[]=New $client = new Client([follow instruction to initialize your client ....]); $response = $client->request('GET', $uri, ['query' => $string]); 

You now have the same name parameters in your query.

Manure.

Source: http_build_query with the same name parameters

+4
source

The answer seems to be here .

I wanted to do something like ?status[]=first&status[]=second

You can do this in Guzzle, as shown in the link above:

 $client = new Client('http://test.com/api'); $request = $client->get('/resource'); $query = $request->getQuery(); $query->set('status', array('first', 'second')); 
+2
source

All Articles