Fix for XML RPC example for Drupal 6.x Services

  1. <?php
  2.  
  3. /**
  4.  * Function for generating a random string, used for
  5.  * generating a token for the XML-RPC session
  6.  */
  7. function getUniqueCode($length = "") {
  8.   $code = md5(uniqid(rand(), true));
  9.   if ($length != "") return substr($code, 0, $length);
  10.   else return $code;
  11. }
  12.  
  13.  
  14. // set local domain
  15. $domain = $_SERVER['SERVER_NAME'];
  16.  
  17. // set API key
  18. // this API key will be provided by Driving Force
  19. $kid = '5988f5ba1c1f31d55db08b5e6c919707';
  20.  
  21.  
  22.  
  23. /*
  24.  * Firstly we touch the system.connect service
  25.  * to open a PHP/Drupal session
  26.  */
  27.  
  28. // set vars for this connection
  29. $method_name = 'system.connect';
  30. $required_args = array();
  31.  
  32. // prepare the request
  33. $request = xmlrpc_encode_request(
  34.   $method_name, $required_args
  35. );
  36.  
  37. // prepare the request context
  38. $context = stream_context_create(
  39.   array(
  40.     'http' => array(
  41.       'method' => "POST",
  42.       'header' => "Content-Type: text/xml",
  43.       'content' => $request,
  44.     )
  45.   )
  46. );
  47.  
  48. // connect
  49. $connect = file_get_contents("http://drivingforce/services/xmlrpc", false, $context);
  50. // retrieve the result
  51. $response = xmlrpc_decode($connect);
  52.  
  53. // display the result on screen
  54. if (xmlrpc_is_fault($response)) {
  55.     print '<h1>Error</h1>';
  56.     trigger_error("xmlrpc: $response[faultString] ($response[faultCode])");
  57. } else {
  58.     print '<h1>Received</h1>';
  59.     print '<pre>'. htmlspecialchars(print_r($response, true)) .'</pre>';
  60. }
  61.  
  62.  
  63.  
  64.  
  65. /*
  66.  * Now we have a session, we can login to
  67.  * retrieve our article feed
  68.  */
  69.  
  70. // set vars for this connection
  71. $nonce = getUniqueCode("10");
  72. $method_name = 'user.login';
  73. $timestamp = (string) strtotime("now");
  74. $required_args = array();
  75.  
  76. // now prepare a hash
  77. $hash_parameters = array(
  78.   $timestamp,
  79.   $domain,
  80.   $nonce,
  81.   $method_name,
  82. );
  83.  
  84. $hash = hash_hmac("sha256", implode(';', $hash_parameters), $kid);
  85.  
  86. // prepared the arguments for this service
  87. $required_args = array(
  88.   $hash,
  89.   $domain,
  90.   $timestamp,
  91.   $nonce,
  92.   $response['sessid'],
  93. );
  94.  
  95.  
  96. // any user-defined arguments for this service
  97. // user credentials will be provided by Driving Force
  98. $user_args = array(
  99.   0 => 'newspaper',
  100.   1 => 'newspaper',
  101. );
  102.  
  103. // add the arguments to the request
  104. foreach ($user_args as $arg) {
  105.   array_push($required_args, $arg);
  106. }
  107.  
  108. // prepare the request
  109. $request = xmlrpc_encode_request(
  110.   $method_name, $required_args
  111. );
  112.  
  113. // prepare the request context
  114. $context = stream_context_create(
  115.   array(
  116.     'http' => array(
  117.       'method' => "POST",
  118.       'header' => "Content-Type: text/xml",
  119.       'content' => $request,
  120.     )
  121.   )
  122. );
  123.  
  124. // connect
  125. $connect = file_get_contents("http://drivingforce/services/xmlrpc", false, $context);
  126. // retrieve the result
  127. $response = xmlrpc_decode($connect);
  128.  
  129. // display the result on screen
  130. if (xmlrpc_is_fault($response)) {
  131.   print '<h1>Error</h1>';
  132.   trigger_error("xmlrpc: $response[faultString] ($response[faultCode])");
  133. } else {
  134.   print '<h1>Received</h1>';
  135.   print '<pre>'. htmlspecialchars(print_r($response, true)) .'</pre>';
  136.  
  137.   // SAVE OUR USER OBJECT - we'll need it later
  138.   $user = new stdClass();
  139.   $user = (object) $response['user'];
  140.  
  141.   // ALSO SAVE OUR LOGGED IN SESSID - we'll need it to logout
  142.   // sessid changes after we login
  143.   $loggedinsessid = $response['sessid'];
  144. }
  145.  
  146.  
  147.  
  148.  
  149. /*
  150.  * Now let's retrieve our specific feed
  151.  */
  152.  
  153. // set vars for this connection
  154. $nonce = getUniqueCode("10");
  155. $method_name = 'views.get';
  156. $timestamp = (string) strtotime("now");
  157. $required_args = array();
  158.  
  159. // now prepare a hash
  160. $hash_parameters = array(
  161.   $timestamp,
  162.   $domain,
  163.   $nonce,
  164.   $method_name,
  165. );
  166.  
  167. $hash = hash_hmac("sha256", implode(';', $hash_parameters), $kid);
  168.  
  169. // prepared the arguments for this service
  170. $required_args = array(
  171.   $hash,
  172.   $domain,
  173.   $timestamp,
  174.   $nonce,
  175.   // note, this is now the logged in sessid returned by user.login
  176.   $response['sessid'],
  177. );
  178.  
  179.  
  180. // any user-defined arguments for this service
  181. $user_args = array(
  182.   0 => 'subscriptions',
  183.   1 => 'feed_1',
  184.   2 => array(),
  185.   3 => array((int) $user->uid),
  186.   4 => 0,
  187.   5 => 0,
  188.   6 => 1,
  189. );
  190.  
  191. // add the arguments to the request
  192. foreach ($user_args as $arg) {
  193.   array_push($required_args, $arg);
  194. }
  195.  
  196. // prepare the request
  197. $request = xmlrpc_encode_request(
  198.   $method_name, $required_args
  199. );
  200.  
  201. // prepare the request context
  202. $context = stream_context_create(
  203.   array(
  204.     'http' => array(
  205.       'method' => "POST",
  206.       'header' => "Content-Type: text/xml",
  207.       'content' => $request,
  208.     )
  209.   )
  210. );
  211.  
  212. // connect
  213. $connect = file_get_contents("http://drivingforce/services/xmlrpc", false, $context);
  214. // retrieve the result
  215. $response = xmlrpc_decode($connect);
  216.  
  217. // display the result on screen
  218. if (xmlrpc_is_fault($response)) {
  219.   print '<h1>Error</h1>';
  220.   trigger_error("xmlrpc: $response[faultString] ($response[faultCode])");
  221. } else {
  222.   print '<h1>Received</h1>';
  223.   print '<pre>'. htmlspecialchars(print_r($response, true)) .'</pre>';
  224. }
  225.  
  226.  
  227.  
  228. /*
  229.  * Finally, logout your logged in user session
  230.  */
  231.  
  232. // set vars for this connection
  233. $nonce = getUniqueCode("10");
  234. $method_name = 'user.logout';
  235. $timestamp = (string) strtotime("now");
  236. $required_args = array();
  237.  
  238. // now prepare a hash
  239. $hash_parameters = array(
  240.   $timestamp,
  241.   $domain,
  242.   $nonce,
  243.   $method_name,
  244. );
  245.  
  246. $hash = hash_hmac("sha256", implode(';', $hash_parameters), $kid);
  247.  
  248. // prepared the arguments for this service
  249. $required_args = array(
  250.   $hash,
  251.   $domain,
  252.   $timestamp,
  253.   $nonce,
  254.   // note, this is now the sessid we saved after the user.login method above
  255.   $loggedinsessid,
  256. );
  257.  
  258. // prepare the request
  259. $request = xmlrpc_encode_request(
  260.   $method_name, $required_args
  261. );
  262.  
  263. // prepare the request context
  264. $context = stream_context_create(
  265.   array(
  266.     'http' => array(
  267.       'method' => "POST",
  268.       'header' => "Content-Type: text/xml",
  269.       'content' => $request,
  270.     )
  271.   )
  272. );
  273.  
  274. // connect
  275. $connect = file_get_contents("http://drivingforce/services/xmlrpc", false, $context);
  276. // retrieve the result
  277. $response = xmlrpc_decode($connect);
  278.  
  279. // display the result on screen
  280. if (xmlrpc_is_fault($response)) {
  281.     print '<h1>Error</h1>';
  282.     trigger_error("xmlrpc: $response[faultString] ($response[faultCode])");
  283. } else {
  284.     print '<h1>Received</h1>';
  285.     print '<pre>'. htmlspecialchars(print_r($response, true)) .'</pre>';
  286. }
  287.  
  288.  
  289.  
  290.  
  291. /*
  292.  * All Done!
  293.  */
  294.  
  295. ?>

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.