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

问题描述 投票:16回答:2

我读了this answer,但我相信有更好的方法在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
        ]
]);
php guzzle
2个回答
24
投票

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

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

7
投票

正确答案的另一种变化:

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

然后打电话给你的请求:

$response = $guzzle_client->request('GET','/api.com',$params);
© www.soinside.com 2019 - 2024. All rights reserved.