Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sending array via query string in guzzle

Guzzle client creates by default from this code

$client->get('https://example.com/{?a}', array('a' => array('c','d')));

this url

https://example.com/?a=c,d

What is the best practice to send array in query string in RESTful application? The question is, how can I determine on the server side whether c,d is a string or an array? Isn't it better to send arrays using square brackets, e.g. a[]=c&a[]=d? How can I set Guzzle to use square brackets? Or it is better to use JSON encoded variables? On the server side I'm using Tonic.

like image 806
Peter Krejci Avatar asked Dec 18 '12 08:12

Peter Krejci


4 Answers

Guzzle has a helper function you can use called build_query(). This uses PHP's http_build_query().

Here's an example of how to use it:

$params = [
    'a[]' => [
        'c',
        'd'
    ],
    'page' => 1
];

$query = \GuzzleHttp\Psr7\build_query($params);

$response = $client->request('GET', 'https://example.com/', [
    'query' => $query
]);
like image 55
Anna Avatar answered Oct 07 '22 00:10

Anna


I'm not 100% sure this quite answers the question. But I found the question while looking for how to construct complex queries using Guzzle and none of the answers here were the solution I ended up using. I'm adding it here in case it's ever useful for any other devs.

Using Guzzle 6, you can do this type of request:

    $endPoint = "https://example.com";

    $queryParams = [
        'a' => [
            [
                "b" => "c"
            ]
        ]
    ];

    $options = [
        'debug' => true, // so you can see what the request looks like
        'query' => $queryParams
    ];

    $client->request('GET', $endPoint, $options);

As a real world example, query params like this:

    $queryParams = [
        'filters' => [
            [
                "field" => "status",
                "value" => "open",
                "operator" => "equal"
            ],
            [
                "field" => "total",
                "operator" => "greater_than",
                "value" => 50
            ],
        ],
        'limit' => 500,
        'start' => 7
    ];

produce a url like this:

https://example.com?filters=[{"field":"status","operator":"equal","value":"open"},{"field":"total","operator":"less_than","value":50}]&limit=500&start=7

The point being that the query key of the $options array, seems very powerful. I'd recommend having a play with that before going down the route of writing complex regular expressions.

like image 34
DazBaldwin Avatar answered Oct 07 '22 01:10

DazBaldwin


Working Solution:

$vars = array('state[]' => array('Assigned','New'), 'per_page' => $perPage, 'page' => $pageNumber);
$query = http_build_query($vars, null, '&');
$string = preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '=', $query); // state[]=Assigned&state[]=New
$client = new Client([follow instruction to initialize your client ....]);
$response = $client->request('GET', $uri, ['query' => $string]);

Now you have same name parameters in your request.

Dung.

Source: http_build_query with same name parameters

like image 30
Dung Avatar answered Oct 07 '22 01:10

Dung


It seems the answer is here.

I wanted to do something like ?status[]=first&status[]=second

You can do this in Guzzle like shown in the link above:

$client = new Client('http://test.com/api');    
$request = $client->get('/resource');    
$query = $request->getQuery();    
$query->set('status', array('first', 'second'));
like image 26
holographic-principle Avatar answered Oct 07 '22 01:10

holographic-principle