首页 > 解决方案 > Symfony HttpClient GET 请求具有多个具有相同名称的查询字符串参数

问题描述

我正在尝试以以下格式发出 API 请求:

/api/v1/courses?enrollment_state=active&include[]=total_students&include[]=term

如何使用HttpClient组件查询字符串参数执行此操作?

$response = $client->request('GET', '/api/v1/courses', [
      'query' => [
           'enrollment_state' => 'active',
           'include[]' => 'term',
           'include[]' => 'total_students',
       ],
]);

由于重复的数组键,上述方法不起作用?

我也试过:

'include[]' => ['term', 'total_students']

标签: phpsymfonysymfony4symfony-http-client

解决方案


创建等效于:

https://www.example.com/?token=foo&abc[]=one&abc[]=two

做就是了:

$client->request(
    'GET',
    'https://www.example.com/',
    [
        'query' => [
            'token' => 'foo',
            'abc' => ['one', 'two']
        ]
    ]
);

推荐阅读