How can I attach a file using PHP cURL XML Call

I am using the Amazon AIMS API to load an inventory file, and I had a problem calling cURL to load the file. The documentation is very limited, so there is no sample code that helps with this.

This is what I still called cURL:

// $FILENAME is filename of the CSV file being uploaded:

$inventory = fopen($FILENAME, 'r') or die("Can't open file!");
echo $inventory;

curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt($ch, CURLOPT_INFILE, $inventory);
curl_setopt($ch, CURLOPT_POSTFIELDS, ''); 
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($filename));
curl_setopt($ch, CUROPT_PUT, TRUE);


$response = curl_exec($ch);
curl_close($ch);

I tried putting $ inventory in CURLOPT_POSTFIELDS and not have INFILE, but I get the same error.

In the XML response, I get a " NO_FILE_ATTACHED", so the obvious problem is attaching the file to the XML call.

I also tried loading, as the first responder said, using example # 2 on the curl_setopt page on php.net.

For this, I used the following code:

$data = array('@/tmp/amazon_export.csv');

curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 


$response = curl_exec($ch);
curl_close($ch);

I got the same answer NO_FILE_ATTACHED.

Any ideas?

+3
3

:

$hCurl = curl_init();
curl_setopt($hCurl, CURLOPT_PUT,            true);
curl_setopt($hCurl, CURLOPT_HEADER,         true);
curl_setopt($hCurl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($hCurl, CURLOPT_CONNECTTIMEOUT, CURL_TIMEOUT_SECS);
curl_setopt($hCurl, CURLOPT_URL,            "$oMessage->url/att/$fid");
curl_setopt($hCurl, CURLOPT_HTTPHEADER,     $aCurlHeaders);
// TODO it could be possible that fopen() would return an invalid handle or not work altogether.  Should handle that
$fp = fopen ($finfo['tmp_name'], "r");
curl_setopt($hCurl, CURLOPT_INFILE,         $fp);
curl_setopt($hCurl, CURLOPT_INFILESIZE,     $finfo['size']);

$sResp = curl_exec($hCurl);
+3

PUT POST , . № 2 curl_setopt POST.

0

you have $ filename and $ FILENAME, the call to filesize should be in your filesize file ($ FILENAME) ...

Hope that helps

0
source

Source: https://habr.com/ru/post/1712724/


All Articles