You are here

function _restclient_multipart_encode in RESTClient 7.2

Build the multipart encoded header data

Parameters

array $data [reference]: Array of parameters to encode. Also the return data.

string $boundary [optional]: Boundary string

If not value is specified, a string will be generated

1 call to _restclient_multipart_encode()
_restclient_prepare_post_data in ./restclient.module
Encodes and sanitizes and body data

File

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

Code

function _restclient_multipart_encode(&$data, $boundary = NULL) {

  // Copy the current parameters into a new variables
  // We use the $data reference to store the result. We use a reference to
  // avoid creating copies of large variables in memory. The file encode
  // function loads a complete file into memory as a string. We have to be
  // careful to avoid hitting memory limits.
  $params = $data;

  // Reset the data container
  $data = '';

  // Check the boundary text
  if (is_null($boundary)) {
    $boundary = uniqid("", TRUE);
  }
  foreach ($params as $key => $value) {

    // Check the type of data to be encoded
    if ($key == 'fid') {

      // @todo add support for an array of fids
      $file_encode = _restclient_multipart_encode_file($value);

      // Check the encoding
      if (FALSE !== $file_encode) {

        // Build the data
        $data .= "--{$boundary}\r\n";
        $data .= $file_encode;
        $data .= "\r\n--{$boundary}--";

        // Clean up some memory
        unset($file_encode);
      }
    }
    else {
      $text_encode = _restclient_multipart_encode_text($key, $value);

      // Check the encoding
      if (FALSE !== $text_encode) {

        // Build the data
        $data .= "--{$boundary}\r\n";
        $data .= $text_encode;

        // Clean up some memory
        unset($text_encode);
      }
    }
  }

  // Check that the data isn't empty and mark the last boundary
  if (!empty($data)) {
    $data .= "--{$boundary}--";
  }
}