【问题标题】:Multiple values in query查询中的多个值
【发布时间】:2018-08-13 08:53:21
【问题描述】:

我正在尝试获取类别的统计信息,我想在其中指定多个类别。我有这样的查询参数

$query_params = json_decode('{
          "aggregated_by": "day", 
          "limit": 500,
          "categories": "category1 category2",
          "start_date": "2018-08-12", 
          "end_date": "2018-08-13"}');

显然我定义多个类别的方式是错误的,但正确的方式是什么?我尝试了数组,逗号/空格分隔的值,但没有运气。

【问题讨论】:

    标签: sendgrid sendgrid-api-v3


    【解决方案1】:

    您只需命名each category in a distinct argument。在您的示例中,它可能看起来像:

    $query_params = json_decode('{ "aggregated_by": "day", "limit": 500, "categories": "category1", "categories": "category2", "start_date": "2018-08-12", "end_date": "2018-08-13"}');

    【讨论】:

    • 我可以发誓它有效,但现在它看起来返回的 STDObject 只有最新的类别参数,最后一个之前的所有类别参数都被忽略
    【解决方案2】:

    所以我通过将参数作为数组传递来解决这个问题:

    $query = array('categories' => ['cat1','cat2']);
    

    然后我修改了 sendgrid 客户端 buildUrl 方法,如下所示:

    private function buildUrl($queryParams = null)
        {
            $path = '/' . implode('/', $this->path);
            if (isset($queryParams)) {
                $queryParams = \http_build_query($queryParams);
                $path .= '?' . preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '=', $queryParams);
            }
            return sprintf('%s%s%s', $this->host, $this->version ?: '', $path);
        }
    

    我添加了 preg_replace 部分,因此正确构建了 url,而不包括嵌套数组中的数组键。

    【讨论】:

      猜你喜欢
      • 2015-05-21
      • 2010-12-13
      • 1970-01-01
      • 1970-01-01
      • 2015-02-21
      • 2015-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多