Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
399 views
in Technique[技术] by (71.8m points)

php - Export HTTP POST request with file from Postman not working

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...