phpsymfonysymfony4symfony-http-client

Symfony HttpClient GET request with multiple query string parameters with same name


I am trying to make an API request in the following format:

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

How can I do so using the HttpClient Component query string parameters?

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

As the above approach does not work due to duplicate array key?

I have also tried:

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

Solution

  • To create the equivalent to:

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

    Just do:

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