I'm trying to automate my Amazon order processing. Currently I'm using Amazon Merchant Transport to get the XML file for an order, but eventually, I'll want to get the xml files with a web-service directly to my seller central account.
Partial array with Amazon order info -
Array
(
...
[37] => Array
(
[tag] => NAME
[type] => complete
[level] => 6
[value] => John Smith
)
...
[39] => Array
(
[tag] => ADDRESSFIELDONE
[type] => complete
[level] => 6
[value] => 100 N MAIN St.
)
...
[41] => Array
(
[tag] => CITY
[type] => complete
[level] => 6
[value] => PLANO
)
...
[43] => Array
(
[tag] => STATEORREGION
[type] => complete
[level] => 6
[value] => TX
)
...
[45] => Array
(
[tag] => POSTALCODE
[type] => complete
[level] => 6
[value] => 75075-1948
)
)
I am able to retrieve my orders as XML documents and can parse them with the following code.
$contents = file_get_contents("amzn-order-sample.xml");
$p = xml_parser_create();
xml_parse_into_struct($p, $contents, $vals, $index);
xml_parser_free($p);
Now I'm trying to pull out the order related information - first the one time information (shipper name, address)
then I want to be able to get the 'items ordered/quantity' information, which could be 1->many items for this customer.
Sample code that I have working -- to retrieve the value (which I will put into another array for further processing) (pretty ugly)
$arrIt = new RecursiveIteratorIterator(new RecursiveArrayIterator($vals));
// shipper name
foreach ($arrIt as $sub) {
$subArray = $arrIt->getSubIterator();
if ($subArray['tag'] === 'NAME') {
$outputArray[] = iterator_to_array($subArray);
echo "Shipper Name\n";
print_r($outputArray);
}
}
// shipper phone
foreach ($arrIt as $sub) {
$subArray = $arrIt->getSubIterator();
if ($subArray['tag'] === 'BUYERPHONENUMBER') {
$outputArray[] = iterator_to_array($subArray);
echo "Shipper Phone\n";
print_r($outputArray);
}
}
// shipper addresslineone
foreach ($arrIt as $sub) {
$subArray = $arrIt->getSubIterator();
if ($subArray['tag'] === 'ADDRESSFIELDONE') {
$outputArray[] = iterator_to_array($subArray);
echo "Shipper Address\n";
print_r($outputArray);
}
}
// shipper city
foreach ($arrIt as $sub) {
$subArray = $arrIt->getSubIterator();
if ($subArray['tag'] === 'CITY') {
$outputArray[] = iterator_to_array($subArray);
echo "Shipper City\n";
print_r($outputArray);
}
}
// shipper state
foreach ($arrIt as $sub) {
$subArray = $arrIt->getSubIterator();
if ($subArray['tag'] === 'STATEORREGION') {
$outputArray[] = iterator_to_array($subArray);
echo "Shipper State\n";
print_r($outputArray);
}
}
// shipper zip
foreach ($arrIt as $sub) {
$subArray = $arrIt->getSubIterator();
if ($subArray['tag'] === 'POSTALCODE') {
$outputArray[] = iterator_to_array($subArray);
echo "Shipper Zip\n";
print_r($outputArray);
}
}
// items
foreach ($arrIt as $sub) {
$subArray = $arrIt->getSubIterator();
if ($subArray['tag'] === 'POSTALCODE') {
$outputArray[] = iterator_to_array($subArray);
echo "Shipper Zip\n";
print_r($outputArray);
}
}
I was going to parse the 1-many items per order in a loop with the above code, but there must be a better, cleaner way.. -- maybe someone has code snippets they can offer.
Thanks in advance for any ideas..
a Newbie to PHP
There must be many better ways to do this type of code in XML and PHP (this will be processed on a Linux server) -- any suggestions or other ideas are greatly welcome