4

I'm trying to send in POST a raw binary file together with some other data using cURL. Basically, I did that:

//Here i create the CURLFile
$cfile = new CURLFile($fullpath,'application/xml', $filename); 

//Data to upload (including the file)
$data_upload = array('username' => $username, 
                     'password' => $password, 
                     'owner' => $owner, 
                     'destination' => $pathpda, 
                     'filename' => $filename, 
                     'filedata' => $cfile);
//Upload
$ch_upload = curl_init($urlws.$e_upload);
curl_setopt($ch_upload, CURLOPT_POST,true);
curl_setopt($ch_upload, CURLOPT_POSTFIELDS, $data_upload);
curl_setopt($ch_upload, CURLOPT_RETURNTRANSFER, true);
$response_upload = curl_exec($ch_upload);
curl_close($ch_upload); 

Everything worked fine. Except that now I want my file to be just binary data and not a real file on disk. Also, I need to send the other data in the request! Does anyone knows how to do that? I looked up in Google but I wasn't able to find a way to do both, sending the raw binary file and the other data. Thanks!

UPDATE To further clarify my question, I don't need to just send random binary data, rather a file whose content comes from ob_get_contents() (for example) so it's not a a physical file on disk. If i just replace the CURLfile to send the binary file data with the "filedata" variable, it simply doesn't work. The web service doesn't recognize the file. Maybe there's a way to create a CURLfile without pointing to a real file.

greg
  • 433
  • 5
  • 12
  • your question is how to send data that is not real file on disc? – fico7489 Nov 02 '15 at 17:45
  • My question is how to send a FILE that has not been saved on the server disk, (eg. a file retrieved from a database or the output of ob_get_contents()) together with other usual post data using cURL. – greg Nov 04 '15 at 11:14
  • By definition of the word `FILE` it is stored on a disk in a `folder`, otherwise it is just content. File and filesystems are just paradigms on how data is stored, you just want to send the content which is nothing different than sending a username string. – Niki van Stein Nov 04 '15 at 11:56
  • @BasvanStein - you don't know what you are talking about. – Anthony Feb 16 '18 at 20:42

2 Answers2

0

Exactly the same way as you do with other variables. In the end everything is send as "binary", or actually in string representation.

So just do

$content = ob_get_contents();
//or file_get_contents() or whatever source
$data_upload = array('username' => $username, 
                 'password' => $password, 
                 'owner' => $owner, 
                 'destination' => $pathpda, 
                 'filename' => $filename, 
                 'filedata' => $cfile,
                 'allkindsofdata' => $content );
Niki van Stein
  • 10,564
  • 3
  • 29
  • 62
  • Thanks for the reply but it just doesn't work. What I'm trying to send is a file, not just binary content. The web service keeps answering "Required request part 'filedata' is not present". I also tried to manually add the header "Content-Type:multipart/form-data" and setting the options CURLOPT_HEADER, CURLOPT_HTTPHEADER, CURLOPT_INFILESIZE with no results. In my first implementation I created a CURLfile. It would be great if I could create the same CURLfile using the output of ob_get_contents() instead of referring to a real file on disk. But I don't think it can be done this way. – greg Nov 04 '15 at 09:32
  • But what do you define as a file, you said the content might come from a file but you do not want to send it as a file right? So basically what you try to do is store a file in a variable and send it? (no?), I suggest you do not send `filename` and `filedata` but just the content as a normal POST variable. – Niki van Stein Nov 04 '15 at 11:54
  • Ok lets make it simple. I need to send to a web service the data in the `$data_upload` variable. If I replace the CURLfile with the `ob_get_contents()` output, the webservice answers with an exception `400,"error":"Bad Request" "Required request part 'filedata' is not present."`. Maybe I need to add an option to the curl_setopt which I don't know. I need help with that. The solution that you proposed unfortunately doesn't work. – greg Nov 04 '15 at 12:30
0

The trick is to make your binary string into a stream in memory and then tell curl to upload that stream, using: CURLOPT_INFILE, CURLOPT_INFILESIZE and CURLOPT_UPLOAD. This answer provides the code to do it.

Community
  • 1
  • 1
gbe
  • 163
  • 6