将查询字符串参数添加到 Guzzle GET 请求?

2022-08-30 11:51:18

我读了这个答案,但我相信有更好的方法可以在Guzzle中创建http url查询,我正在寻找这样的东西,但无法让它正常工作,我也不知道是否有办法转储url字符串以查看它是否正确处理。有人可以告诉我正确的方法吗?

// works correctly
$client = New GuzzleHttp\Client();
$request = $client->get('http://192.168.50.8/foo?-db=database&-lay=layout&-find');
print_r($request->getBody());

不起作用

$request = $client->get($config->Layout['server'], [], [
        'query' => [
            $config->Layout['switches'], // ([ '-db' => 'database', '-lay' => 'layout', '-find' => true)
            $config->Layout['options'], // other params
        ]
]);

答案 1

正确答案的另一种变体:

$params = [
   'query' => [
      'option_1' => string,
      'option_2' => string
   ]
];

然后致电您的请求:

$response = $guzzle_client->request('GET','/api.com',$params);

答案 2

我有同样的问题。我找到了解决方案

public static function getGroupList($current=false) {
$response = self::getRestClient()->get(
    [
        'domains/{domainId}/pricelists',
        ['domainId' => self::getDomainId()]
    ],
    [
        'query' => [
        current => $current
        ]
    ]
);

return new RestResponse($response);

尝试

$response = $client->get(
        [
            $config->Layout['server'],
            []
        ],
        [
            'query' => [
                $config->Layout['switches'], // ([ '-db' => 'database', '-lay' => 'layout', '-find' => true)
                $config->Layout['options'], // other params
            ]
        ]
);

推荐