Multipart PUT upload using Curl and PHP to a REST endpoint - php

I need to HTTP PUT a csv file and some POST fields using multipart POST with PHP and Curl to a REST API endpoint.
The contents of the file upload is stored in a variable $list. The other end point is $url.
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_PUT, true);
$post = array(
//Other Post fields array
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$fh = fopen('php://memory', 'rw');
fwrite($fh, $list);
rewind($fh);
curl_setopt($ch, CURLOPT_INFILE, $fh);
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($list));
$response = curl_exec($ch);
The above code seems to work the only problem is that the other end point requires a specific fieldname for the file upload. How do i set a filename ?
Am i doing something wrong ?
This is the PUT format they have mentioned on API
Content-Disposition: form-data; name="list[csv]"; filename="RackMultipart20110923-63966-hfpyg"
Content-Length: 33
Content-Type: text/csv
Content-Transfer-Encoding: binary
xxxx
yyyy
zzzz
-------------MultipartPost
Content-Disposition: form-data; name="list[list_type]"
Blacklist
-------------MultipartPost--

FYI that is multipart/form-data. You will need to build the body yourself I think, I don't think cURL could be made to build that sort of request with a PUT request. However, this is not a serious problem:
<?php
function recursive_array_mpfd ($array, $separator, &$output, $prefix = '') {
// Recurses through a multidimensional array and populates $output with a
// multipart/form-data string representing the data
foreach ($array as $key => $val) {
$name = ($prefix) ? $prefix."[".$key."]" : $key;
if (is_array($val)) {
recursive_array_mpfd($val, $separator, $output, $name);
} else {
$output .= "--$separator\r\n"
. "Content-Disposition: form-data; name=\"$name\"\r\n"
. "\r\n"
. "$val\r\n";
}
}
}
// This will hold the request body string
$requestBody = '';
// We'll need a separator
$separator = '-----'.md5(microtime()).'-----';
// First add the postfields
$post = array(
//Other Post fields array
);
recursive_array_mpfd($post, $separator, $requestBody);
// Now add the file
$list = "this,is,some,csv,data"; // The content of the file
$filename = "data.csv"; // The name of the file
$requestBody .= "--$separator\r\n"
. "Content-Disposition: form-data; name=\"list[list_type]\"; filename=\"$filename\"\r\n"
. "Content-Length: ".strlen($list)."\r\n"
. "Content-Type: text/csv\r\n"
. "Content-Transfer-Encoding: binary\r\n"
. "\r\n"
. "$list\r\n";
// Terminate the body
$requestBody .= "--$separator--";
// Let's go cURLing...
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: multipart/form-data; boundary="'.$separator.'"'
));
$response = curl_exec($ch);
If you have any problems with this, try echo $requestBody; before the cURL request and make sure it looks like you expect it to.

Related

CURL: Send images with boundary to REST API

Im currently working with some kind of API. I have wrote simple functions which allows me to add new content, however Im stuck on uploading images.
Here's simple CURL command in documentation:
curl -v -s -u username:password \
-H "Content-Type: multipart/form-data" \
-H "Accept: application/api+json" \
-F "image=#img1.jpeg;type=image/jpeg" \
-F "image=#img2.jpeg;type=image/jpeg" \
-XPUT ''
And a sample REQUEST:
PUT /images HTTP/1.1
Host: example
Content-Type: multipart/form-data; boundary=vjrLeiXjJaWiU0JzZkUPO1rMcE2HQ-n7XsSx
--vjrLeiXjJaWiU0JzZkUPO1rMcE2HQ-n7XsSx
Content-Disposition: form-data; name="image"; filename="img1.jpg"
Content-Type: image/jpeg
Content-Transfer-Encoding: binary
Now, there is my function:
$headers_put = array(
"Content-Type: multipart/form-data; boundary=vjrLeiXjJaWiU0JzZkUPO1rMcE2HQ-n7XsSx",
"Accept: application/+json",
);
function putImages($ch, $headers, $ad, $images){
$url = '/images';
$files = [];
foreach($images as $key => $image) {
$number = $key +1;
$paths = parse_url($image, PHP_URL_PATH);
$paths = $_SERVER['DOCUMENT_ROOT'] . $paths;
$cfile = new CURLFile(''. $paths, 'image/jpeg', 'image'.$key);
$files[$key] = $cfile;
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, $files);
$result = curl_exec($ch);
var_dump($result);
echo curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo "\n";
}
Finally, the response is:
{"errors":{"error":{"#key":"unsupported-form-element"}}}"
What Im doing wrong? Any ideas? Thank you kindly for any help.
After several approaches using curl_file_create without getting it to work. I think the mobile.de-API is just implemented badly.
I ended up implementing a custom routine for CURLOPT_POSTFIELDS that is creating the complete multipart manually. I borrowed most of the code from the PHP manpage as "The CURLFile class".
Create an array of the filenames
Create multipart header (see code below)
function curl_custom_postfields(array $files = array()) {
// build file parameters
foreach ($files as $k => $v) {
switch (true) {
case false === $v = realpath(filter_var($v)):
case !is_file($v):
case !is_readable($v):
continue; // or return false, throw new InvalidArgumentException
}
$data = file_get_contents($v);
$body[] = implode("\r\n", array(
"Content-Disposition: form-data; name=\"image\"",
"Content-Type: image/jpeg",
"",
$data,
));
}
// generate safe boundary
do {
$boundary = "---------------------" . md5(mt_rand() . microtime());
} while (preg_grep("/{$boundary}/", $body));
// add boundary for each parameters
array_walk($body, function (&$part) use ($boundary) {
$part = "--{$boundary}\r\n{$part}";
});
// add final boundary
$body[] = "--{$boundary}--";
$body[] = "";
// set options
return array(implode("\r\n", $body), $boundary);
}
Use that function ;)
$postfields = $this->curl_custom_postfields($files);
Add boundary to http header
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data; boundary=' . $postfields[1], 'Accept: application/vnd.de.mobile.api+json'));
Add Postfields
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields[0]);
It's not the cleanest solution at all so please use it with care. But at least it works.
I have the same problem and found a solution but only for single images.
The trick is that your array MUST look like these:
$images = array(
'image' => 'PATH/IMG.jpg',
);
That means that the the key must be "image" and nothing other!
I hope that helps ;)

Telegram BOT Api: how to send a photo using PHP?

The sendPhoto command require an argument photo defined as InputFile or String.
The API doc tells:
Photo to send. You can either pass a file_id as String to resend a photo
that is already on the Telegram servers, or upload a new photo using
multipart/form-data.
And
InputFile
This object represents the contents of a file to be uploaded. Must be
posted using multipart/form-data in the usual way that files are
uploaded via the browser.
So I tried this method
$bot_url = "https://api.telegram.org/bot<bot_id>/";
$url = $bot_url . "sendPhoto?chat_id=" . $chat_id;
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type:multipart/form-data"
));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
"photo" => "#/path/to/image.png",
));
curl_setopt($ch, CURLOPT_INFILESIZE, filesize("/root/dev/fe_new.png"));
$output = curl_exec($ch);
The curls is executed, but Telegram reply this to me:
Error: Bad Request: Wrong persistent file_id specified: contains wrong
characters or have wrong length
I also tried replacing #/path... with a file_get_contents, but in this case Telegram give me an empty reply (and curl_error is empty !).
What the way to send a photo to telegram using php + curl ?
This is my working solution, but it requires PHP 5.5:
$bot_url = "https://api.telegram.org/bot<bot_id>/";
$url = $bot_url . "sendPhoto?chat_id=" . $chat_id ;
$post_fields = array('chat_id' => $chat_id,
'photo' => new CURLFile(realpath("/path/to/image.png"))
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type:multipart/form-data"
));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
$output = curl_exec($ch);
This code helps me alot which I get from php.net website here
Visit http://php.net/manual/en/class.curlfile.php#115161
(Vote Up this code in php website).
I just change headers in this code for telegram bot to send image just copy this function
function curl_custom_postfields($ch, array $assoc = array(), array $files = array()) {
// invalid characters for "name" and "filename"
static $disallow = array("\0", "\"", "\r", "\n");
// build normal parameters
foreach ($assoc as $k => $v) {
$k = str_replace($disallow, "_", $k);
$body[] = implode("\r\n", array(
"Content-Disposition: form-data; name=\"{$k}\"",
"",
filter_var($v),
));
}
// build file parameters
foreach ($files as $k => $v) {
switch (true) {
case false === $v = realpath(filter_var($v)):
case !is_file($v):
case !is_readable($v):
continue; // or return false, throw new InvalidArgumentException
}
$data = file_get_contents($v);
$v = call_user_func("end", explode(DIRECTORY_SEPARATOR, $v));
$k = str_replace($disallow, "_", $k);
$v = str_replace($disallow, "_", $v);
$body[] = implode("\r\n", array(
"Content-Disposition: form-data; name=\"{$k}\"; filename=\"{$v}\"",
"Content-Type: image/jpeg",
"",
$data,
));
}
// generate safe boundary
do {
$boundary = "---------------------" . md5(mt_rand() . microtime());
} while (preg_grep("/{$boundary}/", $body));
// add boundary for each parameters
array_walk($body, function (&$part) use ($boundary) {
$part = "--{$boundary}\r\n{$part}";
});
// add final boundary
$body[] = "--{$boundary}--";
$body[] = "";
// set options
return #curl_setopt_array($ch, array(
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => implode("\r\n", $body),
CURLOPT_HTTPHEADER => array(
"Expect: 100-continue",
"Content-Type: multipart/form-data; boundary={$boundary}", // change Content-Type
),
));
}
Basic Try:Now just use this code by sending photo name with path and chat id
here is it how:-
$array1=array('chat_id'=><here_chat_id>);
$array2=array('photo'=>'index.jpg') //path
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.telegram.org/<bot_token>/sendPhoto");
curl_custom_postfields($ch,$array1,$array2);//above custom function
$output=curl_exec($ch);
close($ch);
For sending png or other methods change curl_custom function according to your need.
I searched a lot online but didn't find the answer. But, your question solved my problem ... I just changed your code and that answered it for me ...
I changed your code to this:
$chat_id=chat Id Here;
$bot_url = "https://api.telegram.org/botYOUR_BOT_TOKEN/";
$url = $bot_url . "sendPhoto?chat_id=" . $chat_id;
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type:multipart/form-data"
));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
"photo" => "#path/to/image.png",
));
curl_setopt($ch, CURLOPT_INFILESIZE, filesize("path/to/image.png"));
$output = curl_exec($ch);
print$output;
You can use this API: https://github.com/mgp25/Telegram-Bot-API
example:
$tg->sendPhoto($chat_id, $image, $caption);
You can use either a stored image or URL.
<?php
$BASH_Command='curl -s -X POST "https://api.telegram.org/bot<YourToken>/sendPhoto?chat_id=<YourID>" -F photo="#/path/to/imagefile.jpeg" -F caption="TheImage" > /dev/null &';
echo exec($BASH_Command);
?>
This a bad idea, but you can use some like that:
#!/bin/bash
set -x
set -e
BDIR=/tmp/${RANDOM}
TG_TOKEN=""
TG_CHAT_ID=
mkdir -p ${BDIR}
chmod -R 777 ${BDIR}
su postgres -c "pg_dumpall -f ${BDIR}/postgre.sql"
tar czf ${BDIR}/${HOSTNAME}.tar.gz /var/lib/grafana/ /etc/grafana/ ${BDIR}/postgre.sql
curl -F caption="$(date)" -F chat_id="${TG_CHAT_ID}" -F document=#"${BDIR}/${HOSTNAME}.tar.gz" https://api.telegram.org/bot${TG_TOKEN}/sendDocument
rm -rf ${DBIR}
I thought I should extend the answer to include uploading from an external url but it still involves a process of saving the image to a folder first. Then I added a caption to the image.
$bot_url = "https://api.telegram.org/bot<bot_id>/";
$url = $bot_url . "sendPhoto?chat_id=" . $chat_id ;
$caption = 'Telegram Image SendPhoto function';
$img = '/path/to/save_image.png'; //local path where image should be saved
/* Get the image from the URL and save to your own path. You need to add
allow_url_fopen=On to your php.ini file for the below code to work */
file_put_contents($img, file_get_contents("https://your_image.com/pic.jpg"));
$post_fields = array('chat_id' => $chat_id,
'photo' => new CURLFile(realpath($img)),
'caption' => $caption
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type:multipart/form-data"
));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
$output = curl_exec($ch);
curl_close($ch); //close curl
That's all!

SalesForce REST API PHP, Not able to see uploaded Attachment

I am trying to upoad attachments to a specific case using REST API which I have successfully completed.
the files are being uploaded to that specific case. and I am using base64_encode to send binary data to SalesForce as they required the binary data to be sent.
but the issue is that when I see the files in the sales force control panel,
all the files are listed there and their size is correct, name is correct etc
but when I view/download any file uploaded with the script it doesn't open. the file shows errror.
ie. when I upload an png image with the rest API, I wont be able to open the image after downloading from the sales force control panel.
Can any one please help?
I think sales force might not decode the uploaded files back from base64_encode, is that right?
Thanks in advance
here is the code
$fp = fopen($file, 'r');
$db_img = fread($fp, filesize($file));
$db_img = addslashes($db_img);
$db_img = base64_encode($db_img);
and then after encoding I am concatenating $db_img within the body element like this
...................
...'.$db_img.'...
.................;
I figured it out myself. I thought I should post the answer as well. I am using the following function to add attachments to the Case Object.
No need to convert to base64 at all
public function add_attachment($case_id, $full_file_path, $file_name) {
$url = $this->instance_url."/services/data/v33.0/chatter/feed-elements";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
$headers = array();
$headers[] = "Authorization: OAuth $this->access_token";
$headers[] = 'Content-Type: multipart/form-data; boundary=a7V4kRcFA8E79pivMuV2tukQ85cmNKeoEgJgq';
$post_text = '--a7V4kRcFA8E79pivMuV2tukQ85cmNKeoEgJgq
Content-Disposition: form-data; name="json"
Content-Type: application/json; charset=UTF-8
{
"body":{
"messageSegments":[
{
"type":"Text",
"text":"Task Attachment"
}
]
},
"capabilities":{
"content":{
"description":"Task Attachment",
"title":"'.$file_name.'"
}
},
"feedElementType":"FeedItem",
"subjectId":"'.$case_id.'"
}
--a7V4kRcFA8E79pivMuV2tukQ85cmNKeoEgJgq
Content-Disposition: form-data; name="feedElementFileUpload"; filename="'.$file_name.'"
Content-Type: image/png
'. file_get_contents($full_file_path).'
--a7V4kRcFA8E79pivMuV2tukQ85cmNKeoEgJgq--';
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_text);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
$response_json = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
//print_r($info);
if ( $status != 201 ) {
$this->errors[] = "Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl);
return FALSE;
}
$this->status = $status;
$this->curl_error = curl_error($curl);
$this->curl_errno = curl_errno($curl);
return json_decode($response_json,TRUE);
}

Uploading a profile picture via PHP API

I am having trouble uploading a profile picture for a user. I keep getting a 404 error, which the API docs tells me indicates the profile can't be found. However, above the code I'll display in a sec, I have code to retrieve the profile, and it does exist for the particular userId I'm using. Additionally:
This is with the PHP SDK
The account I used to authenticate with does have access to edit user profiles
The test image I'm using does exist and I am able to read it
Here's my code. It's a little sloppy, but I'll clean it up once I get this going for this particular test user:
$file = "testimage.jpeg";
$image_data = file_get_contents($file);
// Build our data
$uid = uniqid();
$data = "--" . $uid . "\r\n".
"Content-Disposition: form-data; name=\"profileImage\"; filename=\"profileImage.jpeg\"\r\n".
"Content-Type: image/jpeg\r\n".
"\r\n".
$image_data . "\r\n".
"--" . $uid . "--";
$success = false;
$tryAgain = true;
$numAttempts = 1;
$url = "/d2l/api/lp/1.0/profile/user/".$userId."/image";
$uri = $opContext->createAuthenticatedUri($url, "POST");
curl_setopt($ch, CURLOPT_URL, $uri);
while ($tryAgain && $numAttempts < MAX_NUM_ATTEMPTS) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Disposition: multipart/form-data; boundary='.$uid,
'Content-Length: ' . strlen($data))
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
$responseCode = $opContext->handleResult($response, $httpCode, $contentType);
if ($responseCode == D2LUserContext::RESULT_OKAY) {
$success = true;
$tryAgain = false;
}
elseif ($responseCode == D2LUserContext::RESULT_INVALID_TIMESTAMP) {
// Try again since time skew should now be fixed.
$tryAgain = true;
}
else { // Something bad happened
echo "here:\r\n".
$httpCode.
"\r\n\r\n".
$responseCode;
exit;
}
$numAttempts++;
}
I'm at a loss as to what I'm missing. Any advice would be greatly appreciated. Thanks
Edit: I just noticed this section in the API docs:
Note
In order to use this action, the service must have granted the application specific permission to do so (that is, granted permission to the specific application ID and key to attempt this action).
I'll inquire if our app ID/Key does indeed have permission. I thought it did, but maybe I was given incorrect information. I'll inquire about this.
Here is a function that I use that works. I use the PHP Api to get an user context object and do the rest through curl.
$opContext - from the PHP API
$user_id - from d2l
$filename - image filename on server
$filepath - path to file on server (I have faculty and students in different places)
$filetype - for the mimetype
static function set_user_image($opContext,$user_id,$filename,$filepath,$filetype){
$fp = fopen($filepath.$filename, 'r');
$contents = fread($fp, filesize($filepath.$filename));
fclose($fp);
$random_hash = "xxBOUNDARYxx";
$request ="--".$random_hash."\r\nContent-Type: application/json\r\n\r\n{\"Text\":\"Some comment\", \"HTML\":null}\r\n\r\n--".
$random_hash."\r\nContent-Disposition: form-data; name=\"profileimage\"; filename="."\"$filename\""."\r\nContent-Type: image/$filetype\r\n\r\n".
$contents."\r\n\r\n--".$random_hash;
$length=strlen($request);
$url = $opContext->createAuthenticatedUri("/d2l/api/lp/1.1/profile/user/$user_id/image","POST");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("HTTP/1.1", "Content-Type: multipart/form-data; boundary=xxBOUNDARYxx","Content-Length:".$length));
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
$response = curl_exec($ch);
}

CURL - php vs command line bash

From bash terminal, I successfully executed the following command.
curl -v -L -F file='#/var/www/dev/public_html/sixties.mov' -F title='my video' -F description='this is a video' -F language='eng' -F license='a2be14e1-37d9-11dd-ae16-0800200c9a66' -F country='US' http://johnuser:johnpass#website.com/api/media
Now I want to create a PHP script that uses the phpcurl library to execute an equivalent command. My code below is shown, but it's not working. The http://johnuser:johnpass#website.com/api/media server is giving me a generic error message. I'm pretty sure I'm making a mistake by not passing the right parameters or setting the right flags in my php code. Can anyone tell me what's wrong?
$url = 'http://johnuser:johnpass#website.com/api/media';
$fields = array();
$fields['file'] = '#/var/www/dev/public_html/sixties.mov';
$fields['title'] = 'my video';
$fields['description'] = 'this is a test';
$fields['language'] = 'eng';
$fields['country'] = 'US';
$fields['license'] = 'a2be14e1-37d9-11dd-ae16-0800200c9a66';
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
print_r($result);
The error message I got is {"status":{"message":"typeMismatch ","error":true,"code":500}}
I think this is what you need to be doing, if you want upload a file:
// Parameters
$url = 'http://website.com/api/media';
$username = 'johnuser';
$password = 'johnpass';
$upload_file = '/var/www/dev/public_html/sixties.mov';
// Declare a couple of arrays we will need
$fields = $headers = array();
// Standard POST fields
$fields['title'] = 'my video';
$fields['description'] = 'this is a test';
$fields['language'] = 'eng';
$fields['country'] = 'US';
$fields['license'] = 'a2be14e1-37d9-11dd-ae16-0800200c9a66';
// Boundary string for multipart message
$boundary = '--=-=-'.md5(uniqid()).rand().'-=-=--';
// Start the body with the file to be uploaded
$body = "--$boundary\r\n"
. "Content-Disposition: form-data; name=\"file\"; filename=\"".basename($upload_file)."\"\r\n"
. "Content-Type: application/octet-stream\r\n" // You should put the right MIME type here
. "\r\n"
. file_get_contents($upload_file) . "\r\n";
// Loop the fields and build the rest of the body
foreach ($fields as $name => $value) {
$body .= "--$boundary\r\n"
. "Content-Disposition: form-data; name=\"$name\"\r\n"
. "\r\n"
. "$value\r\n";
}
// Finish the body
$body .= "--$boundary--";
// Add a couple of headers
$headers[] = "Content-Type: multipart/form-data; boundary=\"$boundary\"";
$headers[] = 'Content-Length: ' . strlen($body);
$ch = curl_init();
// Set the cURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
// Execute post
$result = curl_exec($ch);
// Close connection
curl_close($ch);
print_r($result);
POST file uploads are done with a MIME multipart message, using the multipart/form-data sub-type.

Categories