Skip to content
Advertisement

Guzzle Service Description How to have Comma Deliminated Array?

is there a way to automatically have a Guzzle Service Descriptor field, take an array, and it’ll parse it to a comma list?

Data

Array [ "test", "another test" ]

Service Descriptors

{
    "name": "YouTube",
    "baseUrl": "https://www.googleapis.com",
    "apiVersion": "v3",
    "description": "YouTube GData Graph API",
    "operations": {
        "GetVideos": {
            "httpMethod": "GET",
            "uri": "/youtube/v3/videos",
            "parameters": {
                "id": {
                    "type":"array",
                    "location":"query",
                    "required": true
                },
                "part": {
                    "location": "query",
                    "default": "snippet"
                },
                "key": {
                    "location": "query",
                    "default": "{MY KEY}",
                    "static": true
                },
                "maxResults": {
                    "location": "query",
                    "default": 50
                }
            }

        }
    }
}

The field is the id under parameters, I’d like to provide the raw php array of strings to that, and have Guzzle automatically convert that to a comma deliminated list

Right now, I have to do this:

$command = $this->client->getCommand('GetVideos', [
    'id' => implode(",", array_slice($this->id, 0, 50))
]);

Right now, that creates a URL with multiple instances of id

https://www.googleapis.com/youtube/v3/videos?id=test&id=another%20test&part=snippet&key={MY KEY}&maxResults=50

and I want it to come out like:

https://www.googleapis.com/youtube/v3/videos?id=test,another%20test&part=snippet&key={MY KEY}&maxResults=50

Is this possible?

Advertisement

Answer

Guzzle 3 constructs querystrings from a Query object, which has an aggregator. By calling ->setAggregator($aggregator) on the Query, you can override the default behavior. You want to give it an instance of GuzzleHttpQueryAggregatorCommaAggregator.

There isn’t much in the Guzzle docs about it, but there is a brief mention.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement