Skip to content

PHP Client Examples

zuk edited this page Feb 27, 2011 · 7 revisions

Introduction and Requirements

This guide uses the Pest library to talk to Rollcall. Pest is a PHP client for RESTful resources.

Installing the Pest

Pest is available from Github at http://github.com/educoder/pest.

To install it on your machine:

git clone git://github.com/educoder/pest.git

Then make sure it's in your PHP include_path.

Setup

We'll be using PestXML rather than just plain Pest, since Rollcall can return XML-encoded data.

<?php

require 'PestXML.php';
  
$rollcall_site_url = "http://localhost:3000";
$rest = new PestXML($rollcall_site_url);

?>

Basically...

You use the Pest client to send GET, POST, PUT, and DELETE requests to the Rollcall server. These four HTTP request methods correspond to the four CRUD verbs (Create = POST, Retrieve = GET, Update = PUT, Delete = DELETE). The requests are made to Rollcall's resource collection URLs.

Have a look at the Wikipedia article on REST (Representational State Transfer) if you're not familiar with this style of web services.

For example, to retrieve a User with ID 5, you would place a GET call to /users/5.xml:

<?php
$user = $rest->get('/users/5.xml');
?>

PestXML parses the XML responses from Rollcall and returns them as SimpleXML objects. See http://php.net/manual/en/book.simplexml.php.

You can also retrieve a User by their username, like this:

<?php
$user = $rest->get('/users/jdoe.xml');
?>

To create a new User, send a POST request along with the User's data:

<?php
$user = $rest->post('/users.xml', array(
  'user' => array(
    'username' => "jdoe",
    'display_name' => "John Doe",
    'kind' => "Instructor",
    'metadata' => array(
      'hair colour' => 'Brown'
    )
  )
));
?>

For detailed information on all of the available resource URLs and their parameters, have a look at the API Docs.

Complete Usage Examples