【问题标题】:sending array via query string in guzzle通过 guzzle 中的查询字符串发送数组
【发布时间】:2012-12-18 08:34:14
【问题描述】:

Guzzle 客户端默认从此代码创建

$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

【问题讨论】:

  • 我也有兴趣

标签: php rest query-string restful-url guzzle


【解决方案1】:

工作解决方案:

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

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

粪。

来源:http_build_query with same name parameters

【讨论】:

    【解决方案2】:

    看来答案是here

    我想做?status[]=first&status[]=second之类的事情

    您可以在 Guzzle 中执行此操作,如上面的链接所示:

    $client = new Client('http://test.com/api');    
    $request = $client->get('/resource');    
    $query = $request->getQuery();    
    $query->set('status', array('first', 'second'));
    

    【讨论】:

      【解决方案3】:

      Guzzle 有一个您可以使用的辅助函数 build_query()。这使用 PHP 的http_build_query()

      这是一个如何使用它的示例:

      $params = [
          'a[]' => [
              'c',
              'd'
          ],
          'page' => 1
      ];
      
      $query = \GuzzleHttp\Psr7\build_query($params);
      
      $response = $client->request('GET', 'https://example.com/', [
          'query' => $query
      ]);
      

      【讨论】:

      • build_query 现在已弃用,建议的替代方案是 \GuzzleHttp\Psr7\Query::build,它的作用完全相同
      【解决方案4】:

      我不是 100% 确定这完全回答了这个问题。但是我在寻找如何使用 Guzzle 构建复杂查询时发现了这个问题,这里没有一个答案是我最终使用的解决方案。我在这里添加它,以防它对任何其他开发者有用。

      使用 Guzzle 6,您可以执行此类请求:

          $endPoint = "https://example.com";
      
          $queryParams = [
              'a' => [
                  [
                      "b" => "c"
                  ]
              ]
          ];
      
          $options = [
              'debug' => true, // so you can see what the request looks like
              'query' => $queryParams
          ];
      
          $client->request('GET', $endPoint, $options);
      

      作为一个真实的例子,查询参数如下:

          $queryParams = [
              'filters' => [
                  [
                      "field" => "status",
                      "value" => "open",
                      "operator" => "equal"
                  ],
                  [
                      "field" => "total",
                      "operator" => "greater_than",
                      "value" => 50
                  ],
              ],
              'limit' => 500,
              'start' => 7
          ];
      

      生成这样的网址:

      https://example.com?filters=[{"field":"status","operator":"equal","value":"open"},{"field":"total","operator":"less_than","value":50}]&limit=500&start=7

      关键是$options 数组的query 键似乎非常强大。我建议在编写复杂的正则表达式之前先玩一下。

      【讨论】:

        【解决方案5】:
        $query = array('x' => array(
                            'a',
                            'b',
                            'c'
                ));
        
        $query_string = http_build_query($query, null, '&'); //build query string
        
        $query_string = preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '[]=', $query_string); //x[]=a&x[]=b
        
        $response = $guzzle->client->get($path, array('query' => $query_string)); //Make guzzle request
        
        return json_decode($response->getBody()->getContents()); //Return JSON decoded array
        

        这就是你如何处理 x 与 guzzle 中的值数组,使用版本 6 或更高版本测试

        【讨论】:

          猜你喜欢
          • 2020-05-02
          • 1970-01-01
          • 1970-01-01
          • 2018-12-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-09-13
          相关资源
          最近更新 更多