You are here

function httprl_multipart_encoder in HTTP Parallel Request & Threading Library 7

Same name and namespace in other branches
  1. 6 httprl.module \httprl_multipart_encoder()

Multipart encode a data array.

PHP has http_build_query() which will url-encode data. There is no built in function to multipart encode data thus we have this function below.

Parameters

string &$data_stream: Appended with all multi-part headers.

array $data_array: Array of data in key => value pairs.

array $prepend: (optional) key => values pairs to prepend to $data_array.

1 call to httprl_multipart_encoder()
httprl_handle_data in ./httprl.module
If data is being sent out in this request, handle it correctly.

File

./httprl.module, line 889
HTTP Parallel Request Library module.

Code

function httprl_multipart_encoder(&$data_stream, $data_array, $prepend = array()) {
  foreach ($data_array as $key => $value) {
    $key_array = $prepend;
    $key_array[] = $key;
    if (is_array($value)) {
      httprl_multipart_encoder($data_stream, $value, $key_array);
    }
    elseif (is_scalar($value)) {
      $key_string = array_shift($key_array);
      if (!empty($key_array)) {
        $key_string .= '[' . implode('][', $key_array) . ']';
      }
      $data_stream .= '--' . HTTPRL_MULTIPART_BOUNDARY . "\r\n";
      $data_stream .= 'Content-Disposition: form-data; name="' . $key_string . "\"\r\n\r\n";
      $data_stream .= $value . "\r\n";
    }
  }
}