You are here

function services_method_call in Services 7

Same name and namespace in other branches
  1. 5 services.module \services_method_call()
  2. 6 services.module \services_method_call()
  3. 6.2 services.module \services_method_call()

This is the magic function through which all remote method calls must pass.

2 calls to services_method_call()
services_admin_browse_test_submit in ./services_admin_browse.inc
xmlrpc_server_call_wrapper in servers/xmlrpc_server/xmlrpc_server.module

File

./services.module, line 280
@author Services Dev Team

Code

function services_method_call($method_name, $args = array(), $browsing = FALSE) {
  if (is_array($method_name) && isset($method_name['#callback'])) {
    $method = $method_name;
  }
  else {
    $method = services_method_get($method_name);
  }

  // Check that method exists.
  if (empty($method)) {
    return services_error(t('Method %name does not exist', array(
      '%name' => $method_name,
    )), 406);
  }

  // Check for missing args
  $hash_parameters = array();
  foreach ($method['#args'] as $key => $arg) {
    if (!$arg['#optional']) {
      if (!isset($args[$key]) && !is_array($args[$key]) && !is_bool($args[$key])) {
        return services_error(t('Missing required arguments.'), 406);
      }
    }
  }

  // Check authentication
  if ($auth_error = services_auth_invoke('authenticate_call', $method, $method_name, $args)) {
    if ($browsing) {
      drupal_set_message(t('Authentication failed: !message', array(
        '!message' => $auth_error,
      )), 'error');
    }
    else {
      return services_error($auth_error, 401);
    }
  }

  // Load the proper file.
  if ($file = $method['#file']) {

    // Initialize file name if not given.
    $file += array(
      'file name' => '',
    );
    module_load_include($file['file'], $file['module'], $file['file name']);
  }

  // Construct access arguments array
  if (isset($method['#access arguments'])) {
    $access_arguments = $method['#access arguments'];
    if (isset($method['#access arguments append']) && $method['#access arguments append']) {
      $access_arguments[] = $args;
    }
  }
  else {

    // Just use the arguments array if no access arguments have been specified
    $access_arguments = $args;
  }

  // Call default or custom access callback
  if (call_user_func_array($method['#access callback'], $access_arguments) != TRUE) {
    return services_error(t('Access denied'), 401);
  }

  // Change working directory to drupal root to call drupal function,
  // then change it back to server module root to handle return.
  $server_root = getcwd();
  $server_info = services_get_server_info();
  if ($server_info) {
    chdir($server_info->drupal_path);
  }
  $result = call_user_func_array($method['#callback'], $args);
  if ($server_info) {
    chdir($server_root);
  }
  return $result;
}