Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Query string params to a Guzzle GET request?

Tags:

php

guzzle

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         ] ]); 
like image 694
ehime Avatar asked Apr 29 '14 18:04

ehime


People also ask

Can we send query parameters in GET request?

Unless there's something sensitive in the request parameters, it is perfectly fine to send them as part of URL.

CAN GET method have query parameters?

When the GET request method is used, if a client uses the HTTP protocol on a web server to request a certain resource, the client sends the server certain GET parameters through the requested URL. These parameters are pairs of names and their corresponding values, so-called name-value pairs.

Is guzzle better than cURL?

The main benefits of using Guzzle over cURL is the API it offers, which results in more concise and readable code. For example look at the difference between the code in this question and the accepted answer, the cURL code is much more verbose that the Guzzle implementation.


1 Answers

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); 
like image 140
kaleazy Avatar answered Oct 11 '22 06:10

kaleazy