GuzzleHttp中有多个重复的uri参数

问题描述 投票:4回答:1

我正在访问Echo Nest API,它要求我重复相同的uri参数名称bucket。但是我不能在Guzzle 6中使用这个工作.I read a similar issue from 2012,但是这种方法不起作用。

我尝试将其手动添加到查询字符串中,但没有成功。

API调用示例可以是:

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

这是我的示例客户端:

/**
 * @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
            ])
        ]
    );

    // ...
}

你能帮助我吗?

guzzle
1个回答
0
投票

在Guzzle 6中,您不能再通过任何聚合函数。每当你将一个数组传递给query配置它will be serializedhttp_build_query函数:

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

要避免它,您应该自己序列化查询字符串并将其作为字符串传递。

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
        ])
    ...
);

否则你可以传入handler选项一个调整后的HandlerStack,它将在你的堆栈中包含你的中间件处理程序。那个人会读一些新的配置参数,比如query_with_duplicates,构建可接受的查询字符串和modify请求的Uri相应地。

© www.soinside.com 2019 - 2024. All rights reserved.