Multiple duplicate uri parameters in GuzzleHttp
Asked Answered
R

3

6

I am accessing the Echo Nest API, which requires me to repeat the same uri parameter name bucket. However I can't make this work in Guzzle 6. I read a similar issue from 2012, however the approach does not work.

I have tried adding it manually into the query string without any success.

A sample API call could be:

http://developer.echonest.com/api/v4/song/search?format=json&results=10&api_key=someKey&artist=Silbermond&title=Ja&bucket=id:spotify&bucket=tracks&bucket=audio_summary

Here's my example Client:

/**
 * @param array $urlParameters
 * @return Client
 */
protected function getClient()
{
    return new Client([
        'base_uri' => 'http://developer.echonest.com/api/v4/',
        'timeout'  => 5.0,
        'headers' => [
            'Accept' => 'application/json',
        ],
        'query' => [
            'api_key' => 'someKey',
            'format' => 'json',
            'results' => '10',
            'bucket' => 'id:spotify'         // I need multiple bucket parameter values with the 'bucket'-name
    ]);
}

/**
 * @param $artist
 * @param $title
 * @return stdClass|null
 */
public function searchForArtistAndTitle($artist, $title)
{
    $response = $this->getClient()->get(
        'song/search?' . $this->generateBucketUriString(),
        [
            'query' => array_merge($client->getConfig('query'), [
                'artist' => $artist,
                'title' => $title
            ])
        ]
    );

    // ...
}

Can you help me?

Recuperator answered 12/11, 2015 at 13:7 Comment(0)
E
4

I had the same need today, but now we are on Guzzle 7, the easiest way of getting duplicates for params (bucket=value1&bucket=value2&bucket=value3...) is to use the Query Build method. For this to work do the following:

// Import the class
use GuzzleHttp\Psr7\Query;

Example params

$params = [
  'bucket' => ['value1','value2','value3'],
];

Then when passing the params array to the query key, first pass it through the Query::build method

$response = $client->get('/api', [
    'query' => Query::build($params),
]);
Edythedythe answered 8/2, 2023 at 7:33 Comment(0)
I
2

In the Guzzle 6 you are not allowed to pass any aggregate function anymore. Whenever you will pass an array to the query config it will be serialized with the http_build_query function:

if (isset($options['query'])) {
    $value = $options['query'];
    if (is_array($value)) {
        $value = http_build_query($value, null, '&', PHP_QUERY_RFC3986);
    }

To avoid it you should serialize a query string by your own and pass it as string.

new Client([
    'query' => $this->serializeWithDuplicates([
        'bucket' => ['id:spotify', 'id:spotify2']
    ]) // serialize the way to get bucket=id:spotify&bucket=id:spotify2
...
$response = $this->getClient()->get(
    ...
        'query' => $client->getConfig('query').$this->serializeWithDuplicates([
            'artist' => $artist,
            'title' => $title
        ])
    ...
);

Otherwise you could pass into the handler option an adjusted HandlerStack that will have in its stack your Middleware Handler. The one will read some new config param, say, query_with_duplicates, build acceptable Query String and modify Request's Uri with it accordingly.

Inexertion answered 28/12, 2017 at 12:36 Comment(0)
B
0

For the above request , please follow the steps :

  $queryParams = [
      'format' => 'json',
      'results' => '10',
      'api_key' => 'someKey',
      'artist'='Silbermond',
      'title'=>'Ja',
      'bucket' => 'id:spotify,tracks,audio_summary'
 ];

 $response = $this->getClient()->get(


   ...

         'query' => $queryParams

   ...
   );

Please note the parameters to be duplicated are to be added as a comma-separated string rather than as an array .

If you would add as an array , the resultant parameters would look like

...&bucket[]=id:spotify&bucket[]=tracks&bucket[]=audio_summary...

Bradbury answered 23/6, 2023 at 8:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.