Skip to content

Instantly share code, notes, and snippets.

@mcorrigan
Last active October 10, 2022 10:42
Show Gist options
  • Save mcorrigan/f7032dc9ca5d78034b18 to your computer and use it in GitHub Desktop.
Save mcorrigan/f7032dc9ca5d78034b18 to your computer and use it in GitHub Desktop.
Creating a POST request using file handlers for file content
<?php
# base code found here: http://stackoverflow.com/questions/3085990/post-a-file-string-using-curl-in-php
$delimiter = '----WebKitFormBoundary'.uniqid();
$data = create_post($delimiter, array(
'bingo' => 'yes'
), array(
'file' => array(
'filename' => 'data.txt',
'type' => 'application/octet-stream',
'content' => '@data.txt',
//'content' => fopen('data.txt', 'rb'),
//'content' => 'raw contents',
)
));
$handle = curl_init('http://requestb.in/1717eay1');
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_HTTPHEADER , array(
'Content-Type: multipart/form-data; boundary=' . $delimiter,
'Content-Length: ' . strlen($data)));
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
curl_exec($handle);
/**
* @param array $postFields The KVPs you want as post data
* @param array $fileFields The objects of each file: name => array(type=>'mime/type',content=>'raw data|resource',filename=>'file.csv')
*/
function create_post($delimiter, $postFields, $fileFields = array()){
// form field separator
$eol = "\r\n";
$data = '';
// populate normal fields first (simpler)
foreach ($postFields as $name => $content) {
$data .= "--$delimiter" . $eol;
$data .= 'Content-Disposition: form-data; name="' . $name . '"';
$data .= $eol.$eol; // note: double endline
$data .= $content;
$data .= $eol;
}
// populate file fields
foreach ($fileFields as $name => $file) {
$data .= "--$delimiter" . $eol;
// fallback on var name for filename
if (!array_key_exists('filename', $file))
{
$file['filename'] = $name;
}
// "filename" attribute is not essential; server-side scripts may use it
$data .= 'Content-Disposition: form-data; name="' . $name . '";' .
' filename="' . $file['filename'] . '"' . $eol;
// this is, again, informative only; good practice to include though
$data .= 'Content-Type: ' . $file['type'] . $eol;
// this endline must be here to indicate end of headers
$data .= $eol;
// the file itself (note: there's no encoding of any kind)
if (is_resource($file['content'])){
// rewind pointer
rewind($file['content']);
// read all data from pointer
while(!feof($file['content'])) {
$data .= fgets($file['content']);
}
$data .= $eol;
}else {
// check if we are loading a file from full path
if (strpos($file['content'], '@') === 0){
$file_path = substr($file['content'], 1);
$fh = fopen(realpath($file_path), 'rb');
if ($fh) {
while (!feof($fh)) {
$data .= fgets($fh);
}
$data .= $eol;
fclose($fh);
}
}else {
// use data as provided
$data .= $file['content'] . $eol;
}
}
}
// last delimiter
$data .= "--" . $delimiter . "--$eol";
return $data;
}
@mr-deamon
Copy link

+1 for this snippet, after one day of try&error, this almost instantly worked!

@afandiyusuf
Copy link

+1 for saving my live

@peterez
Copy link

peterez commented Dec 12, 2019

thank you a lot

@LucasHang
Copy link

saved

@snpstudios
Copy link

how to save the boundary picture ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment