1

I have a Postman HTTP request using POST, a form data field of a file saved under the key plsm_xls_file[]. The file is in the local filesystem.

This request runs perfectly from POSTMAN but when I try to export it to PHP-Curl from the Code Snippets I get something like this:


$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://mydomain.nl/po_upload3.php?xlsimport=2',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS => array('plsm_xls_file[]'=> new CURLFILE('/C:/Users/myuser/Documents/vita_debug/201216_FG_PC_68715.xlsx'),'template_id' => '170'),
  CURLOPT_HTTPHEADER => array(
    'cookie: PHPSESSID=509e15pepo3ok80nd74jhdis33;'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

It's not working. It's like the file isn't properly attached to the HTTP request.

EDIT: I finally understood that the problem was that POSTMAN has access to my filesystem but the remote server where I tried to run the exported snippet don't- A very silly mistake on my side.

Hacciano
  • 30
  • 5

1 Answers1

0

I had this problem a while back, and while there was never a true resolution (even in PHP bug tracker), I was able to fix it by not including the file in the setopt_array command.

PHP 7.2 CURLFile Gives "Invalid Filename" Warning

In short, try taking your CURLOPT_POSTFIELDS option out of the curl_setopt_array call and add this:

curl_setopt($curl, CURLOPT_POSTFIELDS, array('plsm_xls_file[]'=> new CURLFILE('/C:/Users/myuser/Documents/vita_debug/201216_FG_PC_68715.xlsx'),'template_id' => '170'));
Lawrence Johnson
  • 3,924
  • 2
  • 17
  • 30
  • Still not working. Maybe is my version of PHP? I am using 5.6. I will try taking the rest of options out of the array. Thanks for you help – Hacciano Dec 30 '20 at 15:46
  • In theory it should work in 5.6, but 5.6 EOL was about a year ago, so it's difficult to troubleshoot on my end since I have no instances of it available. Try adding this after `curl_exec` so we can get some info on the response: `print_r(curl_getinfo()); exit(0);` – Lawrence Johnson Dec 30 '20 at 16:10