1

Im trying to post to a multipart html form using CURLs CURLOPT_POSTFIELDS, but I cannot find out how to post it with no files.

When testing the form in normal browser, the "File" field can be empty, "Name" and "Message" can not.

This works:

$array = array(
 "Name" => "Jon",
 "Message" => "My package was broken, please send new.",
 "File" => "@look_omg_its_broken_omgomg_look_at_the_corners.jpg"
);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $array);

This doesnt:

$array = array(
 "Name" => "Jon",
 "Message" => "My package was broken, please send new.",
 "File" => ""
);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $array);
$output = curl_exec($ch);

This last $output results in a bool(false) I have also tried these variations of values, but no success.

"File" => "@"
"File" => "@/"
"File" => NULL
"File"
"File" => array("")
"File" => array("filename" => "")

Anyone know better?

Kristian Rafteseth
  • 2,002
  • 5
  • 27
  • 46

2 Answers2

1

Try just removing the File field entirely? If it is empty it doesn't get submitted?

$array = array(
 "Name" => "Jon",
 "Message" => "My package was broken, please send new."
);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $array);
$output = curl_exec($ch);
Femi
  • 64,273
  • 8
  • 118
  • 148
  • the problem is that the backend is checking for the key, if the "File" field is missing, it produces a stacktrace saying something about referencing a null object. so cannot be done that way :( have to send in the "File" field, but its value must represent that no file is gonna be sent in. it has to be possible, since the webform can do it. (no other async posts or anything) – Kristian Rafteseth Aug 10 '11 at 05:34
  • 1
    Ah. This has been asked before, with no result (see http://stackoverflow.com/questions/5137382/sending-empty-input-type-file-fields-with-curl-on-php). I think you will have to build the *multipart/form-data* body manually as pointed out by @Alix Axel. There's an old old email on the cURL lists that basically says this: http://curl.haxx.se/mail/lib-2005-06/0079.html. Good luck. – Femi Aug 10 '11 at 06:42
1

Without refactoring your backend or emulating a multi-part form submission manually I'm pretty sure you won't be able to solve your problem with any CURLOPT_POSTFIELDS logic alone, also see:

If you insist on trying to solve this using CURL alone, I suggest you inspect the ext/curl source code.

Community
  • 1
  • 1
Alix Axel
  • 151,645
  • 95
  • 393
  • 500