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 ] ]);
Unless there's something sensitive in the request parameters, it is perfectly fine to send them as part of URL.
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With