static function OAuthBodyMultipartFormdata::encodeBody in Lingotek Translation 7.6
Same name and namespace in other branches
- 7.7 lib/oauth-php/library/body/OAuthBodyMultipartFormdata.php \OAuthBodyMultipartFormdata::encodeBody()
- 7.2 lib/oauth-php/library/body/OAuthBodyMultipartFormdata.php \OAuthBodyMultipartFormdata::encodeBody()
- 7.3 lib/oauth-php/library/body/OAuthBodyMultipartFormdata.php \OAuthBodyMultipartFormdata::encodeBody()
- 7.4 lib/oauth-php/library/body/OAuthBodyMultipartFormdata.php \OAuthBodyMultipartFormdata::encodeBody()
- 7.5 lib/oauth-php/library/body/OAuthBodyMultipartFormdata.php \OAuthBodyMultipartFormdata::encodeBody()
Builds the request string.
The files array can be a combination of the following (either data or file):
file => "path/to/file", filename=, mime=, data=
Parameters
array params (name => value) (all names and values should be urlencoded):
array files (name => filedesc) (not urlencoded):
Return value
array (headers, body)
File
- lib/
oauth-php/ library/ body/ OAuthBodyMultipartFormdata.php, line 47
Class
- OAuthBodyMultipartFormdata
- Create the body for a multipart/form-data message.
Code
static function encodeBody($params, $files) {
$headers = array();
$body = '';
$boundary = 'LingotekOAuthRequester_' . md5(uniqid('multipart') . microtime());
$headers['Content-Type'] = 'multipart/form-data; boundary=' . $boundary;
// 1. Add the parameters to the post
if (!empty($params)) {
foreach ($params as $name => $value) {
$body .= '--' . $boundary . "\r\n";
$body .= 'Content-Disposition: form-data; name="' . OAuthBodyMultipartFormdata::encodeParameterName(rawurldecode($name)) . '"';
$body .= "\r\n\r\n";
$body .= urldecode($value);
$body .= "\r\n";
}
}
// 2. Add all the files to the post
if (!empty($files)) {
$untitled = 1;
foreach ($files as $name => $f) {
$data = false;
$filename = false;
if (isset($f['filename'])) {
$filename = $f['filename'];
}
if (!empty($f['file'])) {
$data = @file_get_contents($f['file']);
if ($data === false) {
throw new OAuthException2(sprintf('Could not read the file "%s" for form-data part', $f['file']));
}
if (empty($filename)) {
$filename = basename($f['file']);
}
}
else {
if (isset($f['data'])) {
$data = $f['data'];
}
}
// When there is data, add it as a form-data part, otherwise silently skip the upload
if ($data !== false) {
if (empty($filename)) {
$filename = sprintf('untitled-%d', $untitled++);
}
$mime = !empty($f['mime']) ? $f['mime'] : 'application/octet-stream';
$body .= '--' . $boundary . "\r\n";
$body .= 'Content-Disposition: form-data; name="' . OAuthBodyMultipartFormdata::encodeParameterName($name) . '"; filename="' . OAuthBodyMultipartFormdata::encodeParameterName($filename) . '"' . "\r\n";
$body .= 'Content-Type: ' . $mime;
$body .= "\r\n\r\n";
$body .= $data;
$body .= "\r\n";
}
}
}
$body .= '--' . $boundary . "--\r\n";
$headers['Content-Length'] = strlen($body);
return array(
$headers,
$body,
);
}