2

I'm not much of a PHP person, but trying to port a cli CURL call to PHP ... originally I had:

curl -i -H "Content-Type: text/csv" -X POST --data-binary @$filepath $destination

I've tried to turn this into:

<?php
$data['Filedata'] = "@".$filepath;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $destination.'/_');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/csv"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch); 
curl_close($ch);
?>

The problem is that this results in an extra header being prepended to the destination, containing ------------------------------6614b0cf8aae Content-Disposition: form-data; name=Filedata; filename=$filepath Content-Type: application/octet-stream.

I assumed this was because of the way I used $data['Filedata'], so I tried replacing the CURLOPT_POSTFIELDS line with the below, but that just winds up posting the path string rather than the file itself.

curl_setopt($ch, CURLOPT_POSTFIELDS, "@".$filepath);

Anyone mind pointing out what I'm missing? Like I said, not a PHP person!

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
serilain
  • 441
  • 4
  • 15

1 Answers1

5

Just load the file content directly and it will do. Currently you are doing key/value pared POST data which is different from your original curl commandline.

curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($filepath));
Sabuj Hassan
  • 38,281
  • 14
  • 75
  • 85