tags:

views:

302

answers:

3

I'm using the Amazon AIMS API to upload a an inventory file and I'm having an issue with the cURL call to upload the file. The documentation is very limited, so there is no example code that helps out in this.

This is what I have so far of the cURL call:

// $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've try putting $inventory into the CURLOPT_POSTFIELDS and not having an INFILE, but I get the same error.

On the XML response, I'm getting "NO_FILE_ATTACHED" so the obvious issue is getting the file to be attached to the XML call.

I also tried uploading as the first responder said using the Example #2 on the curl_setopt page on php.net.

For that, 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 NO_FILE_ATTACHED response back.

Any ideas?

A: 

You are combining PUT and POST in a single curl operation, which will not work. Refer to example #2 of the curl_setopt manual page for an example on how to upload a file using POST.

shadowhand
I'm still getting the same NO_FILE_ATTACHED error with this instead:$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); Which is as in the example.
Conor B
Where do you see a POST? This code is in production and used thousands of times a day to attach files to outgoing mail, so I don't think it "will not work".
grantwparks
A: 

This works for me:

$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);
grantwparks
A: 

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

Hope thats helps

Shaun