I am trying to send a POST request to an API and part of that request I am adding a CSV file to get return tokens based on the emails and numbers I provide. The first few lines of code use data that I retrieve from my database, and that as well as generating the file and populating it with text data is correct. The problem is with my cURL. When I make this request in PostMan, the result is perfectly fine, and I actually used the PHP-curl generated code from PostMan to write this request. However, the line
filename=\"".$filename."\"\r\n
does not, for some reason, bring up the file that was just correctly populated before, and the API log says that the file has no data in it. Both files are located in the same directory, so I really am at a loss for what I'm doing incorrectly.
$file = fopen($filename, "w");
while ($row2 = mysqli_fetch_array($query2)){
$txt = "$row2[email], $row2[firstname], $row2[lastname],
$row[company], $row2[mobilephone]\n";
echo $txt;
fwrite($file, $txt);
}
$curl = curl_init();
echo $filename."\n";
curl_setopt_array($curl, array(
CURLOPT_URL => "https://example.com",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\n
Content-Disposition: form-data;
name=\"token\"\r\n\r\12345\r\n
------WebKitFormBoundary7MA4YWxkTrZu0gW\r\n
Content-Disposition: form-data; name=\"datafile\";
filename=\"".$filename."\"\r\n
Content-Type: text/csv\r\n\r\n\r\n
------WebKitFormBoundary7MA4YWxkTrZu0gW--",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Can anyone spot what I am doing incorrectly here?
NOTE: I have replaced several fields and strings with dummy data for privacy/security reasons