我有一个请求,其中大多数输入参数都作为JSON请求对象放入。出于文档目的,我想指定用户可以放入的最常见字段,但是对于JSON请求中的名称值存在很多可变性,我不想记录所有这些,因为它会很麻烦。
以下是我现在所拥有的截图:
作为一个例子,如果我想放入一个名为“people-with”的JSON属性并将其设置为“['joe','paul','jane']那么这在JSON中很容易做到但是我如何在我的PHP / Restler代码中选择它。现在这个服务的签名是:
/**
* ADD an Activity
*
* Add a new action to a user's stream
*
* @url POST /{user_id}
*
* @param integer $user_id The user_id for whom the actions apply; you can insert the text "self" and it will resolve to the current/default user
* @param string $start_time {@from body} The date/time that the activity was started (YYYY-MM-DD or YYYY-MM-DD HH:mm:SS)
* @param string $action {@from body} The action "slug name" that uniquely identifies an action
* @param string $end_time {@from body} The date/time that the activity concluded (YYYY-MM-DD or YYYY-MM-DD HH:mm:SS)
* @param string $app_id {@from body} The application that captured this activity
* @param string $proxy_user_id {@from body} The person who captured this activity for the individual
* @param string $location {@from body} The location information associated with this activity
*/
public function add_action ($user_id, $start_time, $action, $end_time=null, $app_id=null, $proxy_user_id=null, $location=null)
{
// implement
}
P.S。作为旁注,我暂时将此API服务更改为PUT,以避免几天前提出的POST问题,这在使用POST时也影响了我。
答案 0 :(得分:0)
好的,解决这个问题的关键在于$request_data
关联数组。因此,为了实现(关键字段)文档并能够动态地将值接收到POST / PUT服务中,您只需执行此操作:
/**
* ADD an Activity
*
* Add a new action to a user's stream
*
* @url PUT /{user_id}
*
* @param integer $user_id The user_id for whom the actions apply; you can insert the text "self" and it will resolve to the current/default user
* @param string $start_time {@from body} The date/time that the activity was started (YYYY-MM-DD or YYYY-MM-DD HH:mm:SS)
* @param string $action_nm {@from body} The action "slug name" that uniquely identifies an action
* @param string $end_time {@from body} The date/time that the activity concluded (YYYY-MM-DD or YYYY-MM-DD HH:mm:SS)
* @param string $app_id {@from body} The application that captured this activity
* @param string $proxy_user_id {@from body} The person who captured this activity for the individual
* @param string $location {@from body} The location information associated with this activity
*/
public function add_activity ($user_id, $start_time, $action_nm, $end_time=null, $app_id=null, $proxy_user_id=null, $location=null, $request_data=null)
请注意,$ request_data没有@param
,但$ request_data现在挂在函数签名的末尾。现在假设我在请求的JSON中传递了以下'test' : 'me'
。现在我可以在我的处理程序中找到它:
echo $request_data['test']; // prints "me"
这有效,文档看起来也应如此(参见上面的屏幕截图)。
最后要注意的是,对于那些好奇的人,您可以通过$ request_data数组访问所有JSON变量。这意味着:
echo ($request_data['user_id'] === $user_id); // TRUE