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
2
  • Just add them manually in the string without passing second parameter to the GET() method? Commented Jun 27, 2022 at 10:24
  • 1
    Are the URL query params always changing? If yes, stackoverflow.com/a/4784280/8119309 should answer your question. Commented Jun 27, 2022 at 10:52

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
@MukeshRawat Oops! You right, i will edit the answer to correct that issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.