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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23372598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:40:35  来源:igfitidea点击:

Adding Query string params to a Guzzle GET request?

phpguzzle

提问by ehime

I read this answerbut I believe there is a better way to create a http url query in Guzzle, I am looking for something like this, but cannot get it to work correctly, nor do I know if there is a way to dump the url string to see if it is processing correctly. Could someone show me the correct way to do this?

我阅读了这个答案,但我相信有一种更好的方法可以在 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());

Does not work

不起作用

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

回答by user4550315

I have the same problem. I found solution

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

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

return new RestResponse($response);

Try

尝试

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

回答by kaleazy

Another variation of the correct answer:

正确答案的另一种变体:

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

And then call your request:

然后调用您的请求:

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