Home > Back-end >  How to set query parameters with same name as Guzzle?
How to set query parameters with same name as Guzzle?

Time:06-08

I have no problem setting query parameters in Guzzle. But I can't add parameters with the same name. My query params array is below.

$query_params = array(
                'test' => 'abc',
                'test2' => true,
                'limit' => 10,
                'item_id' => '8159'
            );

I want the query parameters to be as follows.

?test=abc&test2=true&limit=10&item_id=8159&item_id=333&item_id=435&item_id=123..

I want to duplicate the item_id parameter as above. And I tested as follows, but this time a bad request was returned from the service. When I looked at the link, I saw that the '=' symbols turned into strange strings like 5BD0%. There was no problem in other parameters. But that's what happened to those with the same name.

...'item_id' => array('123','3243','243')...

The guzzle setting is as follows:

 $response = $this->client->request('GET', $endpoint, array(
                    'headers' => array(
                        'X-API-KEY' => KEY
                    ),
                    'query' => $query_params
                ));

How can i fix this?

CodePudding user response:

All is there : https://www.php.net/manual/fr/function.http-build-query.php (the function http_build_query() is what you need).

CodePudding user response:

I solved the problem. I changed the query params as below. Then I changed the value of the guzzle 'query'.

$query_params = array(
                'test' => 'abc',
                'test2' => true,
                'limit' => 10,
                'item_id' => array('8159','123')
            );
$response = $this->client->request('GET', $endpoint, array(
                    'headers' => array(
                        'X-API-KEY' => KEY
                    ),
                    'query' => Query::build($query_params)
                ));
  • Related