<?php
/**
* Function for generating a random string, used for
* generating a token for the XML-RPC session
*/
function getUniqueCode($length = "") {
$code = md5(uniqid(rand(), true));
if ($length != "") return substr($code, 0, $length);
else return $code;
}
// set local domain
$domain = $_SERVER['SERVER_NAME'];
// set API key
// this API key will be provided by Driving Force
$kid = '5988f5ba1c1f31d55db08b5e6c919707';
/*
* Firstly we touch the system.connect service
* to open a PHP/Drupal session
*/
// set vars for this connection
$method_name = 'system.connect';
$required_args = array();
// prepare the request
$request = xmlrpc_encode_request(
$method_name, $required_args
);
// prepare the request context
$context = stream_context_create(
array(
'http' => array(
'method' => "POST",
'header' => "Content-Type: text/xml",
'content' => $request,
)
)
);
// connect
$connect = file_get_contents("http://drivingforce/services/xmlrpc", false, $context);
// retrieve the result
$response = xmlrpc_decode($connect);
// display the result on screen
if (xmlrpc_is_fault($response)) {
print '<h1>Error</h1>';
trigger_error("xmlrpc: $response[faultString] ($response[faultCode])");
} else {
print '<h1>Received</h1>';
print '<pre>'. htmlspecialchars(print_r($response, true)) .'</pre>';
}
/*
* Now we have a session, we can login to
* retrieve our article feed
*/
// set vars for this connection
$nonce = getUniqueCode("10");
$method_name = 'user.login';
$timestamp = (string) strtotime("now");
$required_args = array();
// now prepare a hash
$hash_parameters = array(
$timestamp,
$domain,
$nonce,
$method_name,
);
$hash = hash_hmac("sha256", implode(';', $hash_parameters), $kid);
// prepared the arguments for this service
$required_args = array(
$hash,
$domain,
$timestamp,
$nonce,
$response['sessid'],
);
// any user-defined arguments for this service
// user credentials will be provided by Driving Force
$user_args = array(
0 => 'newspaper',
1 => 'newspaper',
);
// add the arguments to the request
foreach ($user_args as $arg) {
array_push($required_args, $arg);
}
// prepare the request
$request = xmlrpc_encode_request(
$method_name, $required_args
);
// prepare the request context
$context = stream_context_create(
array(
'http' => array(
'method' => "POST",
'header' => "Content-Type: text/xml",
'content' => $request,
)
)
);
// connect
$connect = file_get_contents("http://drivingforce/services/xmlrpc", false, $context);
// retrieve the result
$response = xmlrpc_decode($connect);
// display the result on screen
if (xmlrpc_is_fault($response)) {
print '<h1>Error</h1>';
trigger_error("xmlrpc: $response[faultString] ($response[faultCode])");
} else {
print '<h1>Received</h1>';
print '<pre>'. htmlspecialchars(print_r($response, true)) .'</pre>';
// SAVE OUR USER OBJECT - we'll need it later
$user = new stdClass();
$user = (object) $response['user'];
// ALSO SAVE OUR LOGGED IN SESSID - we'll need it to logout
// sessid changes after we login
$loggedinsessid = $response['sessid'];
}
/*
* Now let's retrieve our specific feed
*/
// set vars for this connection
$nonce = getUniqueCode("10");
$method_name = 'views.get';
$timestamp = (string) strtotime("now");
$required_args = array();
// now prepare a hash
$hash_parameters = array(
$timestamp,
$domain,
$nonce,
$method_name,
);
$hash = hash_hmac("sha256", implode(';', $hash_parameters), $kid);
// prepared the arguments for this service
$required_args = array(
$hash,
$domain,
$timestamp,
$nonce,
// note, this is now the logged in sessid returned by user.login
$response['sessid'],
);
// any user-defined arguments for this service
$user_args = array(
0 => 'subscriptions',
1 => 'feed_1',
2 => array(),
3 => array((int) $user->uid),
4 => 0,
5 => 0,
6 => 1,
);
// add the arguments to the request
foreach ($user_args as $arg) {
array_push($required_args, $arg);
}
// prepare the request
$request = xmlrpc_encode_request(
$method_name, $required_args
);
// prepare the request context
$context = stream_context_create(
array(
'http' => array(
'method' => "POST",
'header' => "Content-Type: text/xml",
'content' => $request,
)
)
);
// connect
$connect = file_get_contents("http://drivingforce/services/xmlrpc", false, $context);
// retrieve the result
$response = xmlrpc_decode($connect);
// display the result on screen
if (xmlrpc_is_fault($response)) {
print '<h1>Error</h1>';
trigger_error("xmlrpc: $response[faultString] ($response[faultCode])");
} else {
print '<h1>Received</h1>';
print '<pre>'. htmlspecialchars(print_r($response, true)) .'</pre>';
}
/*
* Finally, logout your logged in user session
*/
// set vars for this connection
$nonce = getUniqueCode("10");
$method_name = 'user.logout';
$timestamp = (string) strtotime("now");
$required_args = array();
// now prepare a hash
$hash_parameters = array(
$timestamp,
$domain,
$nonce,
$method_name,
);
$hash = hash_hmac("sha256", implode(';', $hash_parameters), $kid);
// prepared the arguments for this service
$required_args = array(
$hash,
$domain,
$timestamp,
$nonce,
// note, this is now the sessid we saved after the user.login method above
$loggedinsessid,
);
// prepare the request
$request = xmlrpc_encode_request(
$method_name, $required_args
);
// prepare the request context
$context = stream_context_create(
array(
'http' => array(
'method' => "POST",
'header' => "Content-Type: text/xml",
'content' => $request,
)
)
);
// connect
$connect = file_get_contents("http://drivingforce/services/xmlrpc", false, $context);
// retrieve the result
$response = xmlrpc_decode($connect);
// display the result on screen
if (xmlrpc_is_fault($response)) {
print '<h1>Error</h1>';
trigger_error("xmlrpc: $response[faultString] ($response[faultCode])");
} else {
print '<h1>Received</h1>';
print '<pre>'. htmlspecialchars(print_r($response, true)) .'</pre>';
}
/*
* All Done!
*/
?>