php 将多个查询参数传递给restful api的最佳方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2640560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:11:04  来源:igfitidea点击:

What is best way to pass multiple query parameters to a restful api?

phpapirest

提问by DEzra

I am designing RESTful Api's and would like some advice on designing an API where the caller wants to query records based on more than one search parameter.

我正在设计 RESTful Api,并且想要一些关于设计 API 的建议,调用者希望根据多个搜索参数查询记录。

I have only seen restful apis that use one parameter.

我只见过使用一个参数的restful api。

how should i do this?

我该怎么做?

e.g. if i have created a restful api for a list of contacts, how would I format a call that returned all contacts with firstname==bob & surname==smith?

例如,如果我为联系人列表创建了一个宁静的 api,我将如何格式化一个返回所有带有 firstname==bob & surname==smith 的联系人的调用?

I guess it should be a GET because I am retrieving?

我想它应该是 GET 因为我正在检索?

My only thoughts are:

我唯一的想法是:

http://api.myapi.com/contacts/firstname/bob&surname=smith

http://api.myapi.com/contacts/firstname/bob&surname=smith

But that doesn't seem right ;-(

但这似乎不对;-(

Please advice.

请指教。

Also, do any of the php frameworks support this? e.g. symfony, konstrukt etc.

另外,是否有任何 php 框架支持这个?例如 symfony、konstrukt 等。

回答by Matti Virkkunen

I think

我认为

http://api.myapi.com/contacts?firstname=bob&surname=smith

is the way to go if you have a set of parameters, all of which are optional.

如果您有一组参数,所有这些都是可选的,这是一种方法。