You are here

function services_method_call in Services 6.2

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

Invokes a services method.

Parameters

mixed $method_name: The method name or a method definition array.

array $args: Optional. An array containing arguments for the method. These arguments are not matched by name but by position.

bool $browsing: Optional. Whether the call was made by the services browser or not.

Return value

mixed

2 calls to services_method_call()
services_admin_browse_test_submit in ./services_admin_browse.inc
Submit callback for services_admin_browse_test().
xmlrpc_server_call_wrapper in servers/xmlrpc_server/xmlrpc_server.module
Pass XMLRPC server requests to the appropriate services method.

File

./services.module, line 377
Provides a generic but powerful API for exposing web services.

Code

function services_method_call($method_name, $args = array(), $browsing = FALSE) {

  // Allow external modules to log the results of this service call
  module_invoke_all('services_method_call', $method_name, $args, $browsing);

  // If we're dealing with a resource, we need to convert it.
  if (isset($method_name['resource_type']) && is_array($method_name)) {
    module_load_include('inc', 'services', 'services.resource-translation');
    $method_name = _services_resource_controller_as_service($method_name['resource_name'], $method_name['resource_type'], $method_name, $method_name['file']);
  }
  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();
  $missing_parameters = array();
  foreach ($method['args'] as $key => $arg) {
    if (isset($arg['optional']) && !$arg['optional']) {
      if (!isset($args[$key]) && !is_array($args[$key]) && !is_bool($args[$key])) {
        $missing_parameters[] = $arg['name'];
      }
    }
  }
  if (!empty($missing_parameters)) {
    return services_error(t('Missing required arguments: @missing', array(
      '@missing' => implode(', ', $missing_parameters),
    )), 406);
  }

  // Check authentication
  if ($method['auth'] && ($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 $auth_error;
    }
  }

  // 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);
  }

  // Allow external modules to log the results of this service call
  module_invoke_all('services_method_call_results', $method_name, $args, $browsing, $result);
  return $result;
}