Toodledo API implemented in PHP
Posted: March 29th, 2010 | Author: shesek | Filed under: PHP | Tags: api, php api, toodledo, toodledo api | 12 Comments »Toodledo API 2.0 is out, which is not supported by this class. The 1.0 version is depreciated, but still supported for now. I’ll publish a new version when I have some free time.
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, 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:
$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 WTFPL license.