1

I am using guzzle Http client request for GET method, what i am doing i am sending request to some dynamic url which i am fetching from DB and than appending some field into it and sending request Example below :

$this->client->GET("https://webhook.site/bb1ea111-517e-46f5-867e-e956a75753f0?test=id&name=testname", 'query' => [
      'option_1' => string,
      'option_2' => string
   ]);

Now when i send request it goes like below

https://webhook.site/bb1ea111-517e-46f5-867e-e956a75753f0?option_1=string&option_2=string

So it removed default query params from url

Anyone here please let me know what i can update so that it should go like below

https://webhook.site/bb1ea111-517e-46f5-867e-e956a75753f0?test=id&name=testname&option_1=string&option_2=string
Mukesh Rawat
  • 894
  • 2
  • 8
  • 27
  • Just add them manually in the string without passing second parameter to the `GET()` method? – Laralex Jun 27 '22 at 10:24
  • 1
    Are the URL query params always changing? If yes, https://stackoverflow.com/a/4784280/8119309 should answer your question. – pmcpimentel Jun 27 '22 at 10:52

1 Answers1

1

You can use http_build_query function and add it to your query string because Guzzle will replace array in query with the existing ones:

$this->client->GET("https://webhook.site/bb1ea111-517e-46f5-867e-e956a75753f0?test=id&name=testname&" . http_build_query([
    'option_1' => string,
    'option_2' => string
]));

Alternatively, you may extract parameters from existing url and send the complete params in Guzzle:

$url = "https://webhook.site/bb1ea111-517e-46f5-867e-e956a75753f0?test=id&name=testname";
$query = parse_url($url);
parse_str($query, $queryParams);
$params = array_merge($queryParams, [
    'option_1' => string,
    'option_2' => string
]);
$this->client->GET($url, $params);

I personally prefer second option because it is more clear.

Vahid
  • 940
  • 4
  • 14
  • 1
    yes i too like second option it worked well, the only issue i can see is while doing parse_str we need to do parse_str($query,$output) and than merge $output with other query string. – Mukesh Rawat Jun 28 '22 at 14:16
  • @MukeshRawat Oops! You right, i will edit the answer to correct that issue. – Vahid Jul 02 '22 at 08:44