11

Guzzle客户端默认从此代码创建

$client->get('https://example.com/{?a}', array('a' => array('c','d')));

这个网址

https://example.com/?a=c,d

在 RESTful 应用程序中以查询字符串发送数组的最佳实践是什么?问题是,如何在服务器端确定c,d是字符串还是数组?使用方括号发送数组不是更好吗,例如a[]=c&a[]=d?如何将 Guzzle 设置为使用方括号?还是使用 JSON 编码的变量更好?在服务器端,我使用Tonic

4

5 回答 5

5

工作解决方案:

$vars = array('state[]' => array('Assigned','New'), 'per_page' => $perPage, 'page' => $pageNumber);
$query = http_build_query($vars, null, '&');
$string = preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '=', $query); // state[]=Assigned&state[]=New
$client = new Client([follow instruction to initialize your client ....]);
$response = $client->request('GET', $uri, ['query' => $string]);

现在您的请求中有相同的名称参数。

粪。

来源:http_build_query 同名参数

于 2016-02-19T16:07:28.147 回答
4

似乎答案就在这里

我想做类似的事情?status[]=first&status[]=second

您可以在 Guzzle 中执行此操作,如上面的链接所示:

$client = new Client('http://test.com/api');    
$request = $client->get('/resource');    
$query = $request->getQuery();    
$query->set('status', array('first', 'second'));
于 2013-04-12T20:14:16.033 回答
3

Guzzle 有一个你可以使用的辅助函数,叫做build_query(). 这使用 PHP 的http_build_query().

以下是如何使用它的示例:

$params = [
    'a[]' => [
        'c',
        'd'
    ],
    'page' => 1
];

$query = \GuzzleHttp\Psr7\build_query($params);

$response = $client->request('GET', 'https://example.com/', [
    'query' => $query
]);
于 2021-02-05T14:56:25.043 回答
2

我不是 100% 确定这完全回答了这个问题。但是我在寻找如何使用 Guzzle 构建复杂查询时发现了这个问题,这里没有一个答案是我最终使用的解决方案。我在这里添加它以防它对任何其他开发人员有用。

使用 Guzzle 6,您可以执行此类请求:

    $endPoint = "https://example.com";

    $queryParams = [
        'a' => [
            [
                "b" => "c"
            ]
        ]
    ];

    $options = [
        'debug' => true, // so you can see what the request looks like
        'query' => $queryParams
    ];

    $client->request('GET', $endPoint, $options);

作为一个真实的例子,查询参数如下:

    $queryParams = [
        'filters' => [
            [
                "field" => "status",
                "value" => "open",
                "operator" => "equal"
            ],
            [
                "field" => "total",
                "operator" => "greater_than",
                "value" => 50
            ],
        ],
        'limit' => 500,
        'start' => 7
    ];

产生这样的网址:

https://example.com?filters=[{"field":"status","operator":"equal","value":"open"},{"field":"total","operator":"less_than","value":50}]&limit=500&start=7

query关键是数组的键$options,看起来很强大。我建议在编写复杂的正则表达式之前先玩一下。

于 2019-05-31T09:05:36.623 回答
0
$query = array('x' => array(
                    'a',
                    'b',
                    'c'
        ));

$query_string = http_build_query($query, null, '&'); //build query string

$query_string = preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '[]=', $query_string); //x[]=a&x[]=b

$response = $guzzle->client->get($path, array('query' => $query_string)); //Make guzzle request

return json_decode($response->getBody()->getContents()); //Return JSON decoded array

这是您可以x在 guzzle 中处理值数组的方式,并使用版本 6 或更高版本进行了测试

于 2020-04-30T16:16:58.730 回答