#summary Sample code for talking to Taskr using PHP's Zend_Rest_Client #labels Example,Phase-Implementation === Introduction === One way to talk to Taskr in PHP is using the [http://framework.zend.com/manual/en/zend.rest.client.html Zend_Rest_Client] library. The client, part of the [http://framework.zend.com/ Zend_Framework], is an abstraction layer providing an object-oriented API for RESTful resources. The !Zend_Framework is available for download from http://framework.zend.com/download. For general information on using the library, have a look at: * http://framework.zend.com/manual/en/zend.rest.client.html * http://www.pixelated-dreams.com/archives/243-Next-Generation-REST-Web-Services-Client.html === Setup === First, make sure that the Zend framework is installed somewhere in your `include_path`. Then at the top of your php script: {{{ }}} *Authentication* If you have authentication enabled on your Taskr server, you will need to provide the credentials for the client: {{{ $username = 'taskr'; $password = 'task!'; Zend_Rest_Client::getHttpClient()->setAuth(username, $password); }}} Okay, now we're ready to start working with Tasks. === Retrieving the list of all scheduled Tasks === {{{ get("/tasks.xml"); ?> }}} `$tasks` is a SimpleXml object, so calling print_r($result) will let you see all of its data. Here's an example of how to print out all of the tasks as an HTML list: {{{ task) { echo "\n"; } else { echo "

There are no scheduled tasks.

\n"; } ?> }}} === Creating a new Task === First, we will create a simple Task that executes a piece of Ruby code every 10 seconds: {{{ $data = array( 'name' => "My Example Task #".mktime(), 'schedule_method' => "every", 'schedule_when' => "10s", 'action' => array( 'action_class_name' => "Ruby", 'code' => 'puts "Hello World!"' ) ); $task1 = $rest->post('/tasks.xml', $data); }}} Next we will create a Task that will execute two different actions 5 minutes from now -- the first action is just a piece of Ruby code, the other is a REST call to the [[http://howlr.googlecode.com Howlr]] messaging service: {{{ $data = array( 'name' => "Another Example Task #".mktime(), 'schedule_method' => "in", 'schedule_when' => "5m", 'actions' => array( array('action_class_name' => "Ruby", 'code' => 'puts "Sending a message through Howlr..."'), array('action_class_name' => "Howlr", 'url' => "http://howlr.example.foo/messages.xml", 'subject' => "Testing", 'from' => "joe@example.foo", 'recipients' => "sally@example.foo", 'body' => "Just testing!", 'username' => "howlr", 'password' => "howl!") ) ); $task2 = $rest->post('/tasks.xml', $data); }}} Note that the list of actions is specified as an array of associative arrays. Each associative array represents an action, with the `action_class_name` specifying the action type (see BuiltInActionTypes) and the remaining key-value pairs specifying the list of parameters for the action. In this case the first action will execute the Ruby code given in its `code` parameter -- this one just prints out a message to the console. The second is a more complex action, sending a REST request to an external service. Actions are executed one by one, in the given order, although this can be overridden by specifying `order` values for each action array. === Retrieving a specific Task === Retrieve one of the tasks we just created: {{{ id; $rest = new Zend_Rest_Client($taskr_site_url); $task = $rest->get("/tasks/$id.xml"); ?> }}} Now print the Task's name: {{{ name; ?> }}} Print the type of the first action in this task: {{{ {'task-actions'}->{'task-action'}[0]->{'action-class-name'}; ?> }}} === Deleting a Task === Now lets delete the two tasks we created above: {{{ $id1 = $task1->id; $rest->delete("/tasks/$id1.xml"); $id2 = $task2->id; $rest->delete("/tasks/$id2.xml"); }}} Deleting a task automatically unschedules it.