简体   繁体   English

ZF2,将数组作为查询参数传递,尤其是在Zend \\ Http \\ Request中

[英]ZF2, Passing arrays as query parameters, esp in Zend\Http\Request

Question: 题:

Anyone know of a way to cajole the Zend\\Http\\Request (or perhaps it would be in an implementer of Zend\\Stdlib\\ParametersInterface?) into creating urls where array query arg keys don't contain the indexes. 任何人都知道一种哄骗Zend \\ Http \\ Request(或者可能是Zend \\ Stdlib \\ ParametersInterface的实现者)创建URL的方法,其中数组查询arg键不包含索引。

Background: 背景:

I'm attempting to pass an array of values as a query parameter on a GET request using the Zend\\Http\\Request object. 我试图使用Zend \\ Http \\ Request对象将值的数组作为GET请求上的查询参数传递。

...
$httpClient = new Zend\Http\Client(); // cURL adapter setup omitted
$request = new Zend\Http\Request();  // set url, set GET method omitted

$myQueryArgArray = [ 
    'key0' => 'val0',
    'key1' => ['val1', 'val2'],
];
$request->getQuery()->fromArray($myQueryArgArray);

$response = $httpClient->send($request);
...

The cURL adapter is sending the request out the door with a url that looks like this: cURL适配器使用以下网址发送请求到室外:

hostname/path?key0=val0&key1%5B0%5D=val1&key1%5B1%5D=val2

Without the encoding: 没有编码:

hostname/path/?key0=val0&key1[0]=val1&key1[1]=val2

However, the server I'm calling out to fails unless I do NOT pass indexes in the query string. 但是,如果我通过索引查询字符串我打电话外面的服务器出现故障。 That is, before URL-encoding, I can call my API endpoint with a url like: 也就是说,在进行URL编码之前,我可以使用如下网址来调用我的API端点:

hostname/path?key0=val0&key1[]=val1&key1[]=val2

The question (again :): 问题(再次:):

Anyone know of a way to cajole the Zend\\Http\\Request (or perhaps it would be in an implementer of Zend\\Stdlib\\ParametersInterface?) into creating urls where array query arg keys don't contain the indexes. 任何人都知道一种哄骗Zend \\ Http \\ Request(或者可能是Zend \\ Stdlib \\ ParametersInterface的实现者)创建URL的方法,其中数组查询arg键不包含索引。

What I've tried: 我尝试过的

I've tried wrapping my array in a Zend\\Stdlib\\ArrayObject: 我试过将数组包装在Zend \\ Stdlib \\ ArrayObject中:

...
$myQueryArgArray = [ 
    'key0' => 'val0',
    'key1' => new \Zend\StdLib\ArrayObject(['val1', 'val2']),
];
...

Alas, to no avail. las,无济于事。

I know that I accomplish the goal by manually constructing my query string and handing it directly to the Zend\\Http\\Request object, but I'm looking for a better way than creating my own query strings. 我知道我是通过手动构造查询字符串并将其直接传递给Zend \\ Http \\ Request对象来实现该目标的,但是我正在寻找一种比创建自己的查询字符串更好的方法。

This page seems to indicate that there isn't a standard, so I suppose that neither ZF2 nor my api endpoint are doing it wrong: 该页面似乎表明没有标准,因此我想ZF2和我的api端点都没有做到这一点:
How to pass an array within a query string? 如何在查询字符串中传递数组?

I've looked at the sources and the problem is not the Zend\\Http\\Request class but the Zend\\Http\\Client . 我查看了源代码,问题不是Zend \\ Http \\ Request类,而是Zend \\ Http \\ Client

Look at line 843 you see a call to the http_build_query function. 查看第843行,您看到对http_build_query函数的调用。 And on the php.net site you have a solution in the comments: 在php.net网站上,您可以在注释中找到解决方案:

$query = http_build_query($query);
$query = preg_replace('/%5B[0-9]+%5D/simU', '%5B%5D', $query);

So the cleanest solution probably would be to extend the Zend\\Http\\Client class and override the send method. 因此,最干净的解决方案可能是扩展Zend\\Http\\Client类并重写send方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM