You are here

function _restclient_request_with_body in RESTClient 7.2

Requests with body data.

Compatible with POST and PUT

2 calls to _restclient_request_with_body()
restclient_post in ./restclient.module
Make a POST request
restclient_put in ./restclient.module
Make a PUT request

File

./restclient.module, line 446
Defines a standard REST interface to RESTful services

Code

function _restclient_request_with_body(&$resource_path, &$variables = array(), $type = 'POST') {

  // Map variables
  _restclient_map_variables($variables);

  // Prepare any URL parameters
  if (!empty($variables['parameters'])) {
    _restclient_prepare_url_parameters($resource_path, $variables);
  }

  // Set the method
  $variables['method'] = $type;

  // Append raw body data
  if (!empty($variables['body'])) {
    $variables['data'] = $variables['body'];
  }
  else {

    // Prepare any data variables
    _restclient_prepare_post_data($variables);
  }

  // Build the URL
  $url = _restclient_build_resource_path($resource_path, $variables);

  // Authentication
  if (!_restclient_prepare_authentication($variables)) {

    // Already logged.
    return FALSE;
  }
  switch (RESTCLIENT_ACTIVE_LIBRARY) {
    case RESTCLIENT_LIBRARY_CURL:
      $response = chr_curl_http_request($url, $variables);
      break;
    case RESTCLIENT_LIBRARY_DRUPAL:
    default:
      $response = drupal_http_request($url, $variables);
  }

  // Debug output
  if (variable_get("restclient_debug", FALSE)) {

    // Temporarily add the response to the variables for display
    $variables['response'] = $response;
    $variables['resource_path'] = $resource_path;
    _restclient_debug($variables);

    // Cleanup
    unset($variables['response']);
    unset($variables['resource_path']);
  }

  // Log if the response code is Redirection
  if (RESTCLIENT_RESPONSE_REDIRECTION == restclient_response_code($response)) {
    _restclient_watchdog($type, $response->code, 'Redirection', $url, NULL, WATCHDOG_INFO);
  }
  if (!isset($response->error) || (isset($response->errno) and $response->errno == 0)) {

    // No error occured, return the response
    return $response;
  }

  // Log the error
  _restclient_watchdog($type, $response->code, $response->error, $url, $response);

  // Handle authentication (oauth, hybridauth) related errors. The request
  // may have failed due to an expired token, in which case we can re-authenticate
  // and retry this request.
  $retry = _restclient_authentication_request_error($response, $variables);
  if ($retry and empty($variables['auth_retry'])) {
    $variables['auth_retry'] = TRUE;
    return _restclient_request($resource_path, $variables, $type);
  }

  // If error handling is set, return the response anyway, otherwise return FALSE
  if (isset($variables['error_handling']) and $variables['error_handling']) {
    return $response;
  }
  return FALSE;
}