Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building query string programmatically in Guzzle?

Tags:

php

guzzle

In my PHP Guzzle client code, I have something like

$c = new Client('http://test.com/api/1.0/function');

$request = $c->get('?f=4&l=2&p=3&u=5');

but instead I want to have something like:

$request->set('f', 4);
$request->set('l', 2);
$request->set('p', 3);
$request->set('u', 5);

Is it possible in Guzzle? From the documentation and random googling it would seem it is, but I can't find exactly how.

like image 393
Prof. Falken Avatar asked Sep 05 '12 13:09

Prof. Falken


2 Answers

You can:

$c = new Client('http://test.com/api/1.0/function');

$request = $c->get();

$q = $request->getQuery();

$q->set('f', 4);
$q->set('l', 2);
$q->set('p', 3);
$q->set('u', 5);
like image 68
Prof. Falken Avatar answered Oct 02 '22 20:10

Prof. Falken


Guzzle 6 - you could use query option param

// Send a GET request to /get?foo=bar
$client->request('GET', '/get', ['query' => ['foo' => 'bar']]);

http://docs.guzzlephp.org/en/stable/request-options.html#query

like image 45
Vladimir Pak Avatar answered Oct 02 '22 20:10

Vladimir Pak