xmlrpc_example.php
<?php
session_start();
$url = "http://{$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}";
print "<a href=\"$url?state=test\">Test</a><br>\n";
print "<a href=\"$url?state=listimages\">List Available Images</a><br>\n";
print "<a href=\"$url?state=addrequest\">Add request for Maple 10</a><br>\n";
print "<a href=\"$url?state=requeststatus\">Get status of request</a><br>\n";
print "<a href=\"$url?state=connectdata\">Get connection data</a><br>\n";
print "<a href=\"$url?state=endrequest\">End request</a><br>\n";
print "<pre>\n";
if($_GET['state'] == 'test') {
$rc = remoteVCLCall('XMLRPCtest', array('foo'));
print_r($rc);
}
elseif($_GET['state'] == 'listimages') {
$rc = remoteVCLCall('XMLRPCgetImages', array());
print_r($rc);
}
elseif($_GET['state'] == 'addrequest') {
$rc = remoteVCLCall('XMLRPCaddRequest', array(98, 'now', 60));
if($rc['status'] == 'success') {
print "request id is {$rc['requestid']}<br>\n";
$_SESSION['requestid'] = $rc['requestid'];
}
else {
print_r($rc);
}
}
elseif($_GET['state'] == 'requeststatus') {
if(! array_key_exists('requestid', $_SESSION)) {
print "no request created<br>\n";
exit;
}
$rc = remoteVCLCall('XMLRPCgetRequestStatus', array($_SESSION['requestid']));
print "current status of request {$_SESSION['requestid']} is {$rc['status']}";
}
elseif($_GET['state'] == 'connectdata') {
if(! array_key_exists('requestid', $_SESSION)) {
print "no request created<br>\n";
exit;
}
$rc = remoteVCLCall('XMLRPCgetRequestConnectData', array($_SESSION['requestid'], $_SERVER["REMOTE_ADDR"]));
if($rc['status'] == 'ready')
print_r($rc);
else
print "status of request is {$rc['status']}";
}
elseif($_GET['state'] == 'endrequest') {
if(! array_key_exists('requestid', $_SESSION)) {
print "no request created<br>\n";
exit;
}
$rc = remoteVCLCall('XMLRPCendRequest', array($_SESSION['requestid']));
if($rc['status'] == 'error')
print_r($rc);
else {
print "request ended<br>\n";
unset($_SESSION['requestid']);
}
}
print "</pre>\n";
function remoteVCLCall($method, $args) {
$request = xmlrpc_encode_request($method, $args);
$header = "Content-Type: text/xml\r\n";
$header .= "X-User: userid\r\n";
$header .= "X-Pass: password\r\n";
$header .= "X-APIVERSION: 1";
$context = stream_context_create(
array(
'http' => array(
'method' => "POST",
'header' => $header,
'content' => $request
)
)
);
$file = file_get_contents("https://vcl.ncsu.edu/scheduling/index.php?mode=xmlrpccall", false, $context);
$response = xmlrpc_decode($file);
if(xmlrpc_is_fault($response)) {
trigger_error("xmlrpc: {$response['faultString']} ({$response['faultCode']})");
exit;
}
return $response;
}
?>