【问题标题】:how to accept multiple variables in REST API如何在 REST API 中接受多个变量
【发布时间】:2013-04-25 03:33:55
【问题描述】:

我目前正在学习如何使用 rest api 进行移动开发,我只是想知道有没有一种方法可以在 URL 中接受多个变量?

例如:

$app->get('/chara/arena/:id', 'getArea');
//now this code above can accept only 1 prameter now how do i make it to accept 2 parameters?

喜欢:

/chara/arena?idA=102&idB=123

顺便说一句,我正在使用 SLIM 框架。

【问题讨论】:

    标签: php api rest get slim


    【解决方案1】:

    试试这个代码:

    $idA = $app->request()->get('idA');
    $idB = $app->request()->get('idB');
    

    【讨论】:

      【解决方案2】:

      我设法以某种方式创建了一个解决方案: 我不是同时传递 1 个字符的 id,而是同时传递它们

      所以如果我想传递 id 的 1 和 2,我将传递它们 /chara/battle/1:2 并像数组一样使用explode。

       $app->get('/chara/battle/:mid', 'setArena');
      
       function setArena($raw_ids){
            $ids = explode(':', $raw_ids);
              $chara_a_id = $ids[0];
              $chara_b_id = $ids[1];
       }
      

      我不知道这可以这么简单。

      【讨论】:

      • 或:list($chara_a_id, $chara_b_id) = explode(':', $raw_ids);
      • 我不知道有这样的功能,谢谢你@Madbreaks
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-23
      • 1970-01-01
      • 2021-02-25
      • 1970-01-01
      • 2020-01-23
      • 1970-01-01
      相关资源
      最近更新 更多