0

I'm trying to upload an inventory file to Amazon using Curl. I am able to log into my account and onto the inventory upload page but cannot seem to set the correct path since each time the file is uploaded, it indicates Number of records processed from this upload - Zero. I think I have the path wrong. I am using "@/home/path-to-file/file-name.txt" what am I doing wrong? the upload is successful but the system cannot seem to locate the file.

<?php

$URL = 'https://sellercentral.amazon.com/gp/item-manager/ezdpc/uploadInventory.html/ref=ag_invfile_mmap_home';

$ch  = curl_init();

curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);        
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

$page = curl_exec($ch);


$postFields['uploadType']    = 'PriceAndQty';
$postFields['uploadFileName']    = '@/home/path_to_file/inventory_file.txt';

$post = '';

foreach($postFields as $key => $value) {
    $post .= $key . '=' . urlencode($value) . '&';
}

$post = substr($post, 0, -1);

curl_setopt($ch, CURLOPT_REFERER, $URL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

$page = curl_exec($ch); // make request
aynber
  • 22,380
  • 8
  • 50
  • 63

1 Answers1

1

As far as I know, in line

curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

$post needs to be an array if you want to attach the file. Array or string - that makes different form content type.

You can find details in PHP documentation:

As of PHP 5.2.0, value must be an array if files are passed to this option with the @ prefix.

Ranty
  • 3,333
  • 3
  • 22
  • 24
  • $post is an array. Everything works fine and the code executes but I think the path to the file is wrong – Bruce Wayne Nov 21 '12 at 00:23
  • It is not an array, look at your code. You make string `$post` from array `$postFields`, change the line to `curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);` instead and it will work. – Ranty Nov 21 '12 at 11:34
  • If you have doubts about file path, you can use `echo is_file($file_path);`, 1 means you have supplied the correct path, 0 means incorrect – Ranty Nov 21 '12 at 11:54
  • Hi Ranty, take a look at the line: $post = ''; foreach($postFields as $key => $value) { $post .= $key . '=' . urlencode($value) . '&'; } its takes $postFields and turns them into an array – Bruce Wayne Nov 21 '12 at 19:50
  • `$post` is a string. You can do `var_dump($post);` and it will tell you that it is a string. When you do `curl_setopt($ch, CURLOPT_POSTFIELDS, $post);`, it looks at `$post` and sees `String` and IGNORES the file attachment. You need to use `array`, which in your case is `$postFields` – Ranty Nov 21 '12 at 19:53
  • Glad to help. Consider choosing the answer as a solution if you don't need further assistance on this topic. – Ranty Nov 22 '12 at 07:17
  • @Ranty/@Bruce Wayne , can I use the same code for upload an image to the amazon bucket? – ARUN Oct 21 '14 at 13:18