Fix for user.save with Drupal Services 6.x-2.2

  1. /*
  2.  * Now let's retrieve our user to change
  3.  */
  4. // set vars for this connection
  5. $nonce = getUniqueCode("10");
  6. $method_name = 'user.load';
  7. $timestamp = (string) strtotime("now");
  8. $required_args = array();
  9. // now prepare a hash
  10. $hash_parameters = array(
  11.    $timestamp,
  12.    $domain,
  13.    $nonce,
  14.    $method_name,
  15. );
  16. $hash = hash_hmac("sha256", implode(';', $hash_parameters), $kid);
  17. // prepared the arguments for this service
  18. $required_args = array(
  19.    $hash,
  20.    $domain,
  21.    $timestamp,
  22.    $nonce,
  23.    // note, this is now the logged in sessid returned by user.login
  24.    $loggedinsessid,
  25. );
  26. // any user-defined arguments for this method
  27. $user_args = array(
  28.    // if you request a user that does not exist you will get an error
  29.    0 => array('name' => 'test'),
  30. );
  31. // add the arguments to the request
  32. foreach ($user_args as $arg) {
  33.    array_push($required_args, $arg);
  34. }
  35. // prepare the request
  36. $request = xmlrpc_encode_request(
  37.    $method_name, $required_args
  38. );
  39. // prepare the request context
  40. $context = stream_context_create(
  41.    array(
  42.      'http' => array(
  43.        'method' => "POST",
  44.        'header' => "Content-Type: text/xml",
  45.        'content' => $request,
  46.      )
  47.    )
  48. );
  49. // connect
  50. $connect = file_get_contents($endpoint, false, $context);
  51. // retrieve the result
  52. $response = xmlrpc_decode($connect);
  53. // display the result on screen
  54. if (xmlrpc_is_fault($response)) {
  55.    print '<h1>Error</h1>';
  56.    print '<pre>'. htmlspecialchars(print_r($response, true)) .'</pre>';
  57.    trigger_error("xmlrpc: $response[faultString] ($response[faultCode])");
  58. } else {
  59.    print '<h1>Received</h1>';
  60.    print '<pre>'. htmlentities(print_r($response, true)) .'</pre>';
  61.    // save user for manipulation
  62.    $thisuser = $response;
  63. }
  64. /*
  65.  * Now edit the username of the loaded user
  66.  */
  67. // set the new username
  68. $thisuser['name'] = 'newname';
  69. // unset the authenticated user role due to known issue:
  70. http://drupal.org/node/827858
  71. unset($thisuser['roles'][2]);
  72. /*
  73.  * Now save the user array back to Drupal
  74.  */
  75. // set vars for this connection
  76. $nonce = getUniqueCode("10");
  77. $method_name = 'user.save';
  78. $timestamp = (string) strtotime("now");
  79. $required_args = array();
  80. // now prepare a hash
  81. $hash_parameters = array(
  82.    $timestamp,
  83.    $domain,
  84.    $nonce,
  85.    $method_name,
  86. );
  87. $hash = hash_hmac("sha256", implode(';', $hash_parameters), $kid);
  88. // prepared the arguments for this service
  89. $required_args = array(
  90.    $hash,
  91.    $domain,
  92.    $timestamp,
  93.    $nonce,
  94.    // note, this is now the logged in sessid returned by user.login
  95.    $loggedinsessid,
  96. );
  97. // any user-defined arguments for this method
  98. $user_args = array(
  99.    // user array is sent back to Drupal
  100.    0 => $thisuser,
  101. );
  102. // add the arguments to the request
  103. foreach ($user_args as $arg) {
  104.    array_push($required_args, $arg);
  105. }
  106. // prepare the request
  107. $request = xmlrpc_encode_request(
  108.    $method_name, $required_args
  109. );
  110. // prepare the request context
  111. $context = stream_context_create(
  112.    array(
  113.      'http' => array(
  114.        'method' => "POST",
  115.        'header' => "Content-Type: text/xml",
  116.        'content' => $request,
  117.      )
  118.    )
  119. );
  120. // connect
  121. $connect = file_get_contents($endpoint, false, $context);
  122. // retrieve the result
  123. $response = xmlrpc_decode($connect);
  124. // display the result on screen
  125. if (xmlrpc_is_fault($response)) {
  126.    print '<h1>Error</h1>';
  127.    print '<pre>'. htmlspecialchars(print_r($response, true)) .'</pre>';
  128.    trigger_error("xmlrpc: $response[faultString] ($response[faultCode])");
  129. } else {
  130.    print '<h1>Received</h1>';
  131.    print '<pre>'. htmlentities(print_r($response, true)) .'</pre>';
  132. }

Submit Fix

Any tags you'd like to associate with your code, delimitered by commas (example: Views, CCK, Module, etc).
Select the syntax highlighting mode to use.