You are here

function _restclient_prepare_post_data in RESTClient 7.2

Encodes and sanitizes and body data

Parameters

array $variables [reference]: $variables['headers'] Current set of headers $variables['data'] Data to include in the POST request $variables['multipart'] [optional] Treat the data as a multipart request

2 calls to _restclient_prepare_post_data()
_restclient_prepare_put_data in ./restclient.module
Prepare put body data
_restclient_request_with_body in ./restclient.module
Requests with body data.

File

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

Code

function _restclient_prepare_post_data(&$variables) {
  if (!isset($variables['data'])) {
    $variables['data'] = array();
  }

  // Detect content type
  if (empty($variables['headers']['Content-Type'])) {
    $variables['headers']['Content-Type'] = 'application/x-www-form-urlencoded';
  }
  else {

    // @todo detect which content type is set in the headers
    // Encode based on that. If we don't know what it is, default to 'Content-Type' => 'application/x-www-form-urlencoded'
    // If the body is preset, leave the text as is.
  }

  // Check the library in use. If curl is set, let curl handle the post
  // data automatically. Otherwise, parse the data for drupal_http_request
  $body = '';
  if (RESTCLIENT_LIBRARY_DRUPAL == RESTCLIENT_ACTIVE_LIBRARY) {
    if (!empty($variables['multipart']) && FALSE !== $variables['multipart']) {

      // Build the boundary
      $boundary = uniqid();

      // Set the new header data
      $variables['headers']['Content-Type'] = "multipart/form-data; boundary={$boundary}";

      // Build the multipart data
      _restclient_multipart_encode($variables['data'], $boundary);
    }
    else {

      // Build the body string
      foreach ($variables['data'] as $param => $value) {
        $body .= urlencode($param) . '=' . urlencode($value) . '&';
      }

      // Set the value of data to the new string
      $variables['data'] = $body;
    }
  }
  elseif (RESTCLIENT_LIBRARY_CURL == RESTCLIENT_ACTIVE_LIBRARY) {

    // Build the body string
    foreach ($variables['data'] as $param => $value) {
      $body .= urlencode($param) . '=' . urlencode($value) . '&';
    }

    // Check for a file upload and add the '@' sign accordingly
    // @see http://php.net/function.curl-setopt.php
    if (isset($variables['data']['fid'])) {
      $file = file_load($variables['data']['fid']);

      // @todo check the user's access to this file
      if (isset($file->fid) && isset($file->status) && $file->status == 1) {

        // Load the file
        $file_content = file_get_contents(drupal_realpath($file->uri));
        $variables['data']['fid'] = '@' . drupal_realpath($file->uri);
      }
    }

    // Set the value of data to the new string
    $variables['data'] = $body;
  }
}