You are here

function clients_connection_drupal_services_7_3::callMethodArray in Web Service Clients 7.3

Same name and namespace in other branches
  1. 6.2 connections/clients_drupal/clients_drupal.inc \clients_connection_drupal_services_7_3::callMethodArray()
  2. 7.2 connections/clients_drupal/clients_drupal.inc \clients_connection_drupal_services_7_3::callMethodArray()

Call a remote method.

Parameters

$method: The name of the remote method to call.

$method_params: An array of parameters to passed to the remote method. Note that the D5 version of Services does not seem to respect optional parameters; you should pass in defaults (eg an empty string or 0) instead of omitting a parameter.

Return value

Whatever is returned from the remote site.

Overrides clients_connection_base::callMethodArray

File

connections/clients_drupal/clients_drupal.inc, line 132
Contains classes for Client connections handlers.

Class

clients_connection_drupal_services_7_3
Drupal client for services on a Drupal 7 site for Services 7.x-3.x.

Code

function callMethodArray($method, $method_params = array()) {
  $this->method = $method;

  // Connect to the remote system service to get an initial session id to log in with.
  $connect = xmlrpc($this->endpoint, array(
    'system.connect' => array(),
  ));
  $session_id = $connect['sessid'];
  $this
    ->handleXmlrpcError();

  // We may want to call only system.connect for testing purposes.
  if ($method == 'system.connect') {
    return $connect;
  }

  // Log in and get the user's session ID.
  $this
    ->credentialsLoad();
  $username = $this->credentials['username'];
  $password = $this->credentials['password'];
  $login = xmlrpc($this->endpoint, array(
    'user.login' => array(
      $username,
      $password,
    ),
  ));
  $login_session_id = $login['sessid'];
  $login_session_name = $login['session_name'];

  // Set our cookie for subsequent requests.
  $this->cookie = $login_session_name . '=' . $login_session_id;
  $this
    ->handleXmlrpcError();

  // If the requested method is user.login, we're done.
  if ($method == 'user.login') {
    return $login;
  }
  $headers = array(
    // Pass in the login cookie we received previously.
    'Cookie' => $this->cookie,
    // It would appear that with XMLRPC we always need the XCSRF token, no
    // matter what the method.
    'X-CSRF-Token' => $this
      ->getXCSRFToken(),
  );
  $xmlrpc_options = array(
    'headers' => $headers,
  );
  $result = xmlrpc($this->endpoint, array(
    $method => $method_params,
  ), $xmlrpc_options);

  // Throw an exception for errors from the remote call.
  $this
    ->handleXmlrpcError();
  return $result;
}