Matthew Johnson Matthew Johnson - 2 months ago 22
PHP Question

Trello API: get members, attachments, and card information in one call?

I can get data from the Trello API using this:

private function get_card_info($card_id) {
$client = new \GuzzleHttp\Client();
$base = $this->endpoint . $card_id;
$params = "?key=" . $this->api_key . "&token=" . $this->token;
$cardURL = $base . $params;
$membersURL = $base . "/members" . $params;
$attachmentsURL = $base . "/attachments" . $params;

$response = $client->get($cardURL);
$this->card_info['card'] = json_decode($response->getBody()->getContents());

$response = $client->get($membersURL);
$this->card_info['members'] = json_decode($response->getBody()->getContents());

$response = $client->get($attachmentsURL);
$this->card_info['attachments'] = json_decode($response->getBody()->getContents());
}


However, this is broken into three calls. Is there a way to get card information, the member information, and the attachment information all in one call? The docs mention using
&fields=name,id
, but that only seems to limit what's returned from the base call to the
cards
endpoint.

It's absurd to have to hit the API 3 times every time I need card information, but I can't find any examples gathering all that's needed.

Answer

Trello replied to me, and stated that they would have answered much like Vladimir did. However, the only response I got from that was the initial card data, sans attachments and members. However, they also directed me to this blog post that covers batching requests. They apparently removed it from the docs because of the confusion it created.

To summarize the changes, you essentially make a call to /batch, and append a urls GET parameter with a comma separated list of endpoints to hit. The working final version ended up looking like this:

private function get_card_info($card_id) {
    $client =         new \GuzzleHttp\Client();
    $params =         "&key=" . $this->api_key . "&token=" . $this->token;

    $cardURL = "/cards/" . $card_id;
    $members = "/cards/" . $card_id . "/members";
    $attachmentsURL = "/cards/" . $card_id . "/attachments";

    $urls = $this->endpoint . implode(',', [$cardURL, $members, $attachmentsURL]) . $params;

    $response = $client->get($urls);
    $this->card = json_decode($response->getBody()->getContents(), true);
}
Comments