Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony HttpClient GET请求具有多个同名查询字符串参数_Php_Symfony_Symfony4_Symfony Http Client - Fatal编程技术网

Php Symfony HttpClient GET请求具有多个同名查询字符串参数

Php Symfony HttpClient GET请求具有多个同名查询字符串参数,php,symfony,symfony4,symfony-http-client,Php,Symfony,Symfony4,Symfony Http Client,我正在尝试以以下格式发出API请求: /api/v1/courses?enrollment_state=active&include[]=total_students&include[]=term 如何使用HttpClient组件查询字符串参数执行此操作 $response = $client->request('GET', '/api/v1/courses', [ 'query' => [ 'enrollment_state' =

我正在尝试以以下格式发出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']

要创建等效项,请执行以下操作:

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

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

正如@user1392897所说,@yivi snippet返回url查询字符串中的索引

https://www.example.com/?foo[0]=bar&foo[1]=baz
这是因为它使用了
http\u build\u query
内置函数,这是函数行为。你可以阅读这篇文章

一种解决方法是自己从数组中构建查询字符串,并将其附加到url中,
HttpClient->request()
方法的第二个参数

函数createQueryString(数组$queryArray=[]):?字符串
{
$queryString=http_build_query($queryArray,,,,,,,,\PHP_query_RFC3986);
$queryString=preg_replace('/%5B(?[0-9]|[1-9][0-9]+)%5D=/','%5B%5D=',$queryString);///foo[]=x&foo[]=y
返回“”!==$queryString?$queryString:null;
}
$queryArray=[
“abc”=>[“一”,“二”]
];
$queryString=createQueryString($queryArray);
$url='1https://www.example.com/';
if(is_字符串($queryString)){
$url=sprintf(“%s?%s”,$url$queryString);
}
$response=$client->request('GET',$url);

是否尝试只使用一个“include”键并在数组中传递“term”和“total_students”?这种方法的一个问题是,生成的查询字符串的格式实际上是:。您知道如何在没有数字索引的情况下构建此查询吗?