I was wondering if it is possible to post a file – along with other form data – when the file is just a string?
I know that you can post a file that is already on the filesystem by prefixing the filepath with “@”.
However I’d like to bypass creating a temporary file and send just the file as a string, but I am unsure how to construct the request using cURL in PHP.
Cheers
$postFields = array(
'otherFields' => 'Yes'
,'filename' => 'my_file.csv'
,'data' => 'comma seperated content'
);
$options = array(
CURLOPT_RETURNTRANSFER => true
,CURLOPT_SSL_VERIFYPEER => false
,CURLOPT_SSL_VERIFYHOST => 1
,CURLOPT_POSTFIELDS => $postFields
,CURLOPT_HTTPHEADER => array(
'Content-type: multipart/form-data'
)
);
Should be possible: here’s a form, posted through a browser (irrelevant fields omitted):
So, if we build the POST body ourselves and set an extra header or two, we should be able to simulate this:
This way, we’re doing all the heavy lifting ourselves, and trusting cURL not to mangle it.