Last active
October 10, 2022 10:42
-
-
Save mcorrigan/f7032dc9ca5d78034b18 to your computer and use it in GitHub Desktop.
Revisions
-
Michael Corrigan renamed this gist
Feb 18, 2015 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
Michael Corrigan created this gist
Feb 18, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,87 @@ <?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; }