|
PHPZendRestClientExample
Sample code for talking to Taskr using PHP's Zend_Rest_Client
IntroductionOne way to talk to Taskr in PHP is using the Zend_Rest_Client library. The client, part of the 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:
SetupFirst, make sure that the Zend framework is installed somewhere in your include_path. Then at the top of your php script: <?php require 'Zend/Rest/Client.php'; $taskr_site_url = "http://localhost:7007"; ?> 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<?php
$rest = new Zend_Rest_Client($taskr_site_url);
$tasks = $rest->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: <?php
if ($tasks->task) {
echo "<ul>\n";
foreach ($tasks->task as $task) {
echo "\t<li>".$task->name."</li>\n";
echo "\t<ul>\n";
echo "\t\t<li>execute ".$task->{'schedule-method'}." ".$task->{'schedule-when'}."</li>\n";
echo "\t\t<li>created by ".$task->{'created-by'}." on ".$task->{'created-on'}."</li>\n";
echo "\t</ul>\n";
}
echo "</ul>\n";
} else {
echo "<p>There are no scheduled tasks.</p>\n";
}
?>Creating a new TaskFirst, 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 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 TaskRetrieve one of the tasks we just created: <?php
$id = $task1->id;
$rest = new Zend_Rest_Client($taskr_site_url);
$task = $rest->get("/tasks/$id.xml");
?>Now print the Task's name: <?php echo $task->name; ?> Print the type of the first action in this task: <?php echo $task->{'task-actions'}->{'task-action'}[0]->{'action-class-name'}; ?>Deleting a TaskNow 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. |
Sign in to add a comment