/*
* Now let's retrieve our user to change
*/
// set vars for this connection
$nonce = getUniqueCode("10");
$method_name = 'user.load';
$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
$loggedinsessid,
);
// any user-defined arguments for this method
$user_args = array(
// if you request a user that does not exist you will get an error
0 => array('name' => 'test'),
);
// 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($endpoint, false, $context);
// retrieve the result
$response = xmlrpc_decode($connect);
// display the result on screen
if (xmlrpc_is_fault($response)) {
print '<h1>Error</h1>';
print '<pre>'. htmlspecialchars(print_r($response, true)) .'</pre>';
trigger_error("xmlrpc: $response[faultString] ($response[faultCode])");
} else {
print '<h1>Received</h1>';
print '<pre>'. htmlentities(print_r($response, true)) .'</pre>';
// save user for manipulation
$thisuser = $response;
}
/*
* Now edit the username of the loaded user
*/
// set the new username
$thisuser['name'] = 'newname';
// unset the authenticated user role due to known issue:
http://drupal.org/node/827858
unset($thisuser['roles'][2]);
/*
* Now save the user array back to Drupal
*/
// set vars for this connection
$nonce = getUniqueCode("10");
$method_name = 'user.save';
$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
$loggedinsessid,
);
// any user-defined arguments for this method
$user_args = array(
// user array is sent back to Drupal
0 => $thisuser,
);
// 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($endpoint, false, $context);
// retrieve the result
$response = xmlrpc_decode($connect);
// display the result on screen
if (xmlrpc_is_fault($response)) {
print '<h1>Error</h1>';
print '<pre>'. htmlspecialchars(print_r($response, true)) .'</pre>';
trigger_error("xmlrpc: $response[faultString] ($response[faultCode])");
} else {
print '<h1>Received</h1>';
print '<pre>'. htmlentities(print_r($response, true)) .'</pre>';
}