通过在guzzle中的查询字符串发送数组 [英] sending array via query string in guzzle

查看:51
本文介绍了通过在guzzle中的查询字符串发送数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Guzzle 客户端默认根据此代码创建

Guzzle client creates by default from this code

$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 .

What is the best practice to send array in query string in RESTful application? The question is, how can I determine on the server side whether c,d is a string or an array? Isn't it better to send arrays using square brackets, e.g. a[]=c&a[]=d? How can I set Guzzle to use square brackets? Or it is better to use JSON encoded variables? On the server side I'm using Tonic.

推荐答案

工作解决方案:

$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]);

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

Now you have same name parameters in your request.

来源:具有相同名称参数的http_build_query

这篇关于通过在guzzle中的查询字符串发送数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆