PHP and the Trello API

A couple weeks ago I wrote up a bit about a PHP wrapper object I’d written for the Twitter API v1.1.  Since then I’ve been playing with the Trello API a bit, so I figured I’d write that up as well.

The code here is going to look really familiar because I actually wrote my Trello API wrapper object first, so the Twitter one is based on it.  Take a look…

<?php
  class trello_api {
    private $key;
    private $secret;
    private $token;

    public function __construct ($key, $secret, $token) {
      $this->key = $key;
      $this->secret = $secret;
      $this->token = $token;
    }

    public function request ($type, $request, $args = false) {
      if (!$args) {
        $args = array();
      } elseif (!is_array($args)) {
        $args = array($args);
      }

      if (strstr($request, '?')) {
        $url = 'https://api.trello.com' . $request . '&key=' . $this->key . '&token=' . $this->token;
      } else {
        $url = 'https://api.trello.com' . $request . '?key=' . $this->key . '&token=' . $this->token;
      }

      $c = curl_init();
      curl_setopt($c, CURLOPT_HEADER, 0);
      curl_setopt($c, CURLOPT_VERBOSE, 0);
      curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($c, CURLOPT_URL, $url);

      if (count($args)) curl_setopt($c, CURLOPT_POSTFIELDS , http_build_query($args));

      switch ($type) {
        case 'POST':
          curl_setopt($c, CURLOPT_POST, 1);
          break;
        case 'GET':
          curl_setopt($c, CURLOPT_HTTPGET, 1);
          break;
        default:
          curl_setopt($c, CURLOPT_CUSTOMREQUEST, $type);
      }

      $data = curl_exec($c);
      curl_close($c);

      return json_decode($data);
    }
  }
?>

I’ll admit right off the bat that the API secret is really only included for futureproofing.  As of right now, you only need it in order to get the API token, which you can then combine with the API key to make any calls you need.  The constructor could very easily only accept the key and token.

Say you wanted to use this to get the name of your board’s red label. That would look like this:

$trello = new trello_api($key, $secret, $token);

$data = $trello->request('GET', ('/1/boards/' . $board_id . '?fields=labelNames'));
echo $data->labelNames->red;

unset($trello);

We use the request method make a GET call to /1/boards/xxxxxx, where xxxxxx is the ID of the board we want the data for. We include the optional fields=labelNames query because the names of the labels are all the data we want to get back in this case. The request gives us back an object where each label’s name is available.

For the record, I kind of hate that Trello uses the color of each label as an identifier. I’m sure they can back the decision up but it reminds me of the old CSS “rule” about not naming your classes after what they look like, because what happens if you change what they look like?  If you have a class that converts everything to uppercase and bolds it, and you call it uppercase_bold, that works great until you change it to small caps and italics.  That’s a whole other post, I suppose.

Now that we have the text for the red label on our board, say we want to change it. That looks like this:

$trello = new trello_api($key, $secret, $token);

$trello->request('PUT', ('/1/boards/' . $board_id . '/labelNames/red'), array('value' => $text));

unset($trello);

This time it’s a PUT request to /1/boards/xxxxxx/labelNames/red (where, once again, xxxxxx is the board’s ID).  We use the optional $args argument of the request method to pass in a value of $text, where $text is the new label text.

What if you want to use that newly-renamed red label and apply it to a new card? That’s just a POST request to /1/cards.

$trello = new trello_api($key, $secret, $token);

$trello->request('POST', '/1/cards', array('name' => $card_name, 'idList' => $list_id, 'labels' => 'red'));

unset($trello);

We use the optional $args argument again to pass in our array of arguments. We set the name to whatever the value of $card_name is,  idList is set to $list_id (the ID of the list the card will be added to), and the optional labels specifies that we’re applying just the red label to this new card.

Again, I could complain about using the color as an identifier but whatever.

Like my work with the Twitter API, this is hardly groundbreaking stuff.  Just another thing I thought might be useful for people other than myself so I wrote it up.

2 thoughts on “PHP and the Trello API”

    1. It’s only there for futureproofing, so it never actually gets used. You could pass in null and it would be fine.

      That said, this whole thing needs some love. There are probably better ways to do this by now. It works, it’s just not how I’d write it if I were starting from scratch today.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.