Adding Query string params to a Guzzle GET request?

ehime picture ehime · Apr 29, 2014 · Viewed 51.9k times · Source

I read this answer but 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?

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

Answer

user4550315 picture user4550315 · Feb 10, 2015

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