I am trying currently to accomplish the following:
I have a server, where a PHP-Application is running on. I have to extract some files and send them to another server (Django) with a POST request. I am trying to accomplish this by using cURL. I already have a couple of POST requests running and they all work as needed, but as soon as I try to accomplish this with a file I am running into issues.
When I try to send a file from Javascript to my Django Server with Multipart and FormData, everything works.
I tried to use the following code in PHP:
public function httpPost($url, $data, $multipart) {
$curl = curl_init($url);
if ($multipart) {
// curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
// curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
// curl_setopt($curl, CURLOPT_INFILE, $file_handle);
} else {
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
}
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
if ($response === false) {
var_dump(curl_error($curl));
}
curl_close($curl);
}
As you can see from the above code-snippet, I am trying to set the Header to Multipart and set the data accordingly.
I tried the following things already:
- load the file into PHP with fopen/fread then set it in the
$data
array. Then the file also appears kind of on the Django Server, but in therequest.POST
vars and not in therequest.FILES
vars. - load the file into a
CurlFile
. Then only the file path appears on the Django Server
Always when I set multipart, the Django server can't parse the request.BODY
.
Can anybody help here?