RESTful Clients
Demo time!
Synchronize a group from a file.
<?php
#config
$desired_group = 'Design';
$group_file = 'members.txt';
$uri_base = 'https://catalysttools.washington.edu/rest/crowds/';
$version = 'v1';
# build up our client
$GM = new RestClient;
$GM->certPath = '/etc/apache2/ssl/cert.pem';
$GM->keyPath = '/etc/apache2/ssl/key.pem';
# get base url, find version
foreach($GM->getAsXML($uri_base)->xpath('//x:a') as $node) {
if($node == $version){
$uri_base = $node['href'];
break;
}
}
# get grouplist, find group we want
foreach($GM->getAsXML($uri_base)->xpath('//x:a') as $node) {
if($node == $desired_group){
$uri_of_group = $node['href'];
break;
}
}
# get group, list members
print "Old members:\n";
$member_xml = $GM->getAsXML($uri_of_group);
foreach($member_xml->xpath('//x:a[@rel="member"]') as $node) {
print "$node\n";
}
#Locate the node that represents members
foreach($member_xml->xpath('//x:*[@class="members"]') as $node) {
$members_node = $node;
break;
}
#remove any existing members
unset($members_node->li);
#Add new members from file
foreach(file($group_file) as $new_member){
$new_member = trim($new_member);
$new_member_node = $members_node->addChild('a', $new_member);
$new_member_node->addAttribute('rel', 'member');
}
#put back our altered xml
$GM->put($uri_of_group, $member_xml->asXML());
#re-fetch and see if it worked!
print "\nNew members:\n";
$member_xml = $GM->getAsXML($uri_of_group);
foreach($member_xml->xpath('//x:a[@rel="member"]') as $node) {
print "$node\n";
}
exit;
class RestClient {
public $certPath;
public $keyPath;
function get ($uri) {
return $this->Call($uri, "GET", null);
}
function put ($uri, $content) {
return $this->Call($uri, "PUT", $content);
}
function post ($uri) {
return $this->Call($uri, "POST", $content);
}
function delete ($uri) {
return $this->Call($uri, "DELETE", null);
}
// Method pulls the resource via curl and returns a string of the resource
function Call ($uri, $method, $content) {
// Set up HTTP request
$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
// if cert/key attributes are set, use them
if ($this->keyPath AND $this->certPath) {
curl_setopt($ch, CURLOPT_SSLCERT, $this->certPath);
curl_setopt($ch, CURLOPT_SSLKEY, $this->keyPath);
}
// set up body content if needed
if(($method == "POST" || $method == "PUT") && $content){
$content_length = strlen($content)+2;
$headers = array();
array_push($headers, "Content-Type:text/xml");
array_push($headers, "Content-Length:$content_length\r\n");
array_push($headers,"$content");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
// Do the request
$resource=curl_exec($ch);
$info = curl_getinfo($ch);
if ($info['http_code'] != '200') {
print $info['http_code'];
return FALSE;
}
curl_close($ch);
return $resource;
}
// Convienience function to build a parser and run the GET
function getAsXML ($uri) {
$grouplist_xml = new SimpleXMLElement($this->get($uri));
$grouplist_xml->registerXPathNamespace('x', 'http://www.w3.org/1999/xhtml');
return $grouplist_xml;
}
}
?>