You are here

function clients_connection_drupal_services_rest_7::makeRequest in Web Service Clients 7.3

Make a REST request.

Examples: Retrieve a node: makeRequest('node/NID', 'GET'); Update a node: makeRequest('node/NID', 'POST', $data);

Parameters

$resource_path: The path of the resource. Eg, 'node', 'node/1', etc.

$http_method: The HTTP method. One of 'GET', 'POST', 'PUT', 'DELETE'. For an explanation of how the HTTP method affects the resource request, see the Services documentation at http://drupal.org/node/783254.

$data = array(): (Optional) An array of data to pass to the request.

Return value

The data from the request response.

1 call to clients_connection_drupal_services_rest_7::makeRequest()
clients_connection_drupal_services_rest_7::callMethodArray in connections/clients_drupal_rest/clients_drupal_rest.inc
API function to request a remote resource.

File

connections/clients_drupal_rest/clients_drupal_rest.inc, line 209
Contains classes for Client connections handlers.

Class

clients_connection_drupal_services_rest_7
Class for Drupal client connections, REST D7.

Code

function makeRequest($resource_path, $http_method, $data = array()) {

  // Start by assuming we need to log in.
  $login_needed = TRUE;

  // If the cookie is already set, we don't need to log in.
  if (isset($this->cookie)) {
    $login_needed = FALSE;
  }

  // If the service is 'user/register' (or services_entity module's copy), we
  // need to be anonymous.
  // If you want to use this services as an authenticated user, then use the
  // 'user/create' service, of which this is an alias.
  if ($resource_path == 'user/register' || $resource_path == 'entity_user/register') {
    $login_needed = FALSE;

    // Zap any cookie we might have from a previous request.
    $this->cookie = NULL;
  }
  if ($login_needed) {
    $this
      ->userLogin();
  }
  $data = http_build_query($data, '', '&');
  $headers = array(
    'Accept' => 'application/json',
    // Pass in the login cookie we received previously.
    'Cookie' => $this->cookie,
  );

  // Add a CSRF token if the method is one that requires it.
  $non_safe_method = !in_array($http_method, array(
    'GET',
    'HEAD',
    'OPTIONS',
    'TRACE',
  )) || $resource_path == 'user/token';
  if ($non_safe_method) {
    $headers['X-CSRF-Token'] = $this
      ->getXCSRFToken();
  }
  $options = array(
    'headers' => $headers,
    'method' => $http_method,
    'data' => $data,
  );
  $response = drupal_http_request($this->endpoint . '/' . $resource_path, $options);
  $this
    ->handleRestError($response);
  $result = json_decode($response->data);
  return $result;
}