Toodledo API implemented in PHP March 29, 2010
I couldn’t find one, so I decided to write a PHP class that talks with Toodledo API. its not very well documented/commented, but quite simple and works well.
Works on PHP5+ only. Uses the built-in stream wrapper for making HTTP requests (I’ll probably add support for requests with CURL and POST method too), and returns the response as an SimpleXML object. It uses file-system caching for storing the tokens the API gives you (there’s a CACHEDIR const to define where it should be saved).
The usage is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | $t = new Toodledo('user id', 'password', 'app id'); // last parameter is optional // Add task $response = $t->addTask(array( 'title' => 'Finish writing toodledo class in PHP', ... )); $id = (string)$response->added; // Get tasks $tasks = $t->getTasks(array( 'title' => 'foo', 'tag' => 'bar', )); foreach ($tasks->task as $task) { echo (string)$task->title; } |
You can also get the response as raw XML string and as array, by using Toodledo::RAW and Toodledo::ARR as the second argument.
print_r($t->getTasks(array('title' => 'foo'), Toodledo::ARR));
OK. So I must admit – I cheated. I didn’t really implement the entire API, I just wrote one method named request() and used some __call magic to make everything work. basically, you just call $object->APIMethodName(array(‘extra’=>’parameters’)) with whatever method name and parameters and it just build the request accordingly.
When an error occurs (either while making the request or the Toodledo API returns an error), an Exception is thrown. Make sure to catch those.
That’s about it. You can download it here. released under the SMWTFPL license, which means you can do whatever you want with it (modify, redistribute under every license, sell it and pretty much anything else).
Great post, I have considered using Toodledo’s API for my business purposes but have lacked the will to write my own implementation for PHP.
Your contribution is much appreciated.
This thing is looking great! Thanks for taking the time.
However, the getFolders call only returns the folder’s titles. Is there a way to include each folder’s attributes?
That’s really more up to Toodledo’s API, and since I never needed the folder’s attributes, you should probably ask that at their forums
If you look on the first page of the api (http://www.toodledo.com/info/api_doc.php), it says that it returns attributes with the folders when you call ‘getFolders.’
So the information is definitely available in the API. Would it be tricky to implement?
If Toodledo’s API returns it, it should be there. How are you trying to read it? The correct way of accessing those attributes is:
$folders = $t->getFolders();foreach ($folders as $folder) {
$attributes = $folder->attributes();
echo 'id: '.$attributes['id'];
echo 'private: '.$attributes['private'];
}
If it doesn’t work, check the raw response:
echo $t->getFolders(array(), Toodledo::RAW);Is it there?