1
votes

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.

1
what is the error?azibom

1 Answers

0
votes

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'));