12

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.

3 Answers 3

14

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);
Sign up to request clarification or add additional context in comments.

Comments

6

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

1 Comment

Per those docs, this overwrites any existing query string. It can't be sued to edit the string.
0

Have a look at Guzzle documentaton https://docs.guzzlephp.org/en/stable/request-options.html As you can see it has RequestOptions. RequestOptions are constants. They are defined at GuzzleHttp\RequestOptions. You can look at class source code and see all of them right there. Thus, to keep good and professional programming style you can write following source code below, for example

use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;

class DataClass extends BaseClass
{
    const DEFAULT_ACCEPT_HEADER = 'application/json';
    const DEFAULT_CACHE_HEADER = 'no-cache';
    const HOST = 'http://test.com/';
    const ENDPOINT = 'api/1.0/function';
    const TIMEOUT = 2.0;

    private function getData()
    {

        $client = new Client([
                'base_uri' => self::HTTP_HOST,
                'timeout' => self::TIMEOUT
            ]
        );

        $response = $client->request('GET', self::ENDPOINT,
            [
                RequestOptions::HEADERS => [
                    'Accept' => self::DEFAULT_ACCEPT_HEADER,
                    'Cache-Control' => self::DEFAULT_CACHE_HEADER,
                ],
                RequestOptions::QUERY => [
                        'f' => 4,
                        'l' => 2,
                        'p' => 3,
                        'u' => 5
                ]
            ]
        );

        return json_decode($response->getBody(), JSON_OBJECT_AS_ARRAY);
    }

2 Comments

I looked at the documentation because I was confused by your answer. I don't want to subclass and I never mentioned anything about JSON.
You don't have to. Just use what you find useful. I believe there a lot of people who find this code useful how to work with Guzzle. For example, I do not have opportunity to react on answers at stackoverflow but I find something useful often.

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.