Trying to insert an array into an XML feed submission request to Amazon API

I am developing a script that syncs my stock quantities across Amazon, Ebay, and my website. I have now stumbled at the finishing line. I am trying to update my Amazon stock quantities using the SubmitFeedSample.php as provided by Amazon. I can update a single SKU without issue, however I need to know how to update multiple SKU quantities by passing in an array into the request.

I tried the following which did not give me any errors but has not succeeded in updating the quantities:

    $testArray = array();
	$testArray[0][0] = 'P768';
	$testArray[0][1] = 1;
	$testArray[1][0] = 'ULP40';
	$testArray[1][1] = 3;

	$toStr = '';
	for($i = 0, $j = 1; $i < sizeof($testArray); $i++, $j++) {

	    $toStr .= '&lt;Message&gt;' . '&lt;MessageID&gt;' . $j . '&lt;/MessageID&gt;' . '&lt;OperationType&gtUpdate&lt;/OperationType&gt' . '&lt;Inventory&gt;' . '&lt;SKU&gt;' . $testArray[$i][0] . '&lt;/SKU&gt;' . '&lt;Quantity&gt;' . $testArray[$i][1] . '&lt;/Quantity&gt;' . '&lt;/Inventory&gt;' . '&lt;/Message&gt;';
	}

	$feed ='<?xml version="1.0" encoding="UTF-8"?>
	<AmazonEnvelope xsi:noNamespaceSchemaLocation="amzn-envelope.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	    <Header>
	        <DocumentVersion>1.01</DocumentVersion>
	        <MerchantIdentifier>MYMERCHANTID</MerchantIdentifier>
	    </Header>
	    <MessageType>Inventory</MessageType>' . $toStr . 
	'</AmazonEnvelope>';

I am a novice programmer and I’m quite proud, and very surprised, that I’ve managed to get this far. However; I feel like there should be an easy solution for this?

found this using laravel PHP engine.

//AdminController.php

public function submitFeedToAmazonAPI(Request $request)
{
    //Create the XML request
    $xmlData = '<AmazonEnvelope>
        <Header>
            <DocumentVersion>1.01</DocumentVersion>
        </Header>';
    $xmlData .= '<MessageType>Product</MessageType>';
    $xmlData .= '<Message>';
    foreach($request->items as $item)
    {
        $xmlData .= '<MessageID>' . $item['id'] . '</MessageID>';
        $xmlData .= '<OperationType>Update</OperationType>';
        $xmlData .= '<Product>';
        $xmlData .= '<SKU>' . $item['sku'] . '</SKU>';
        $xmlData .= '<StandardProductID>';
        $xmlData .= '<Type>' . $item['type'] . '</Type>';
        $xmlData .= '<Value>' . $item['value'] . '</Value>';
        $xmlData .= '</StandardProductID>';
        $xmlData .= '<ProductTaxCode>' . $item['tax_code'] . '</ProductTaxCode>';
        $xmlData .= '<DescriptionData>';
        $xmlData .= '<Title>' . $item['title'] . '</Title>';
        $xmlData .= '<Brand>' . $item['brand'] . '</Brand>';
        $xmlData .= '<Description>' . $item['description'] . '</Description>';
        $xmlData .= '</DescriptionData>';
        $xmlData .= '</Product>';
    }
    $xmlData .= '</Message>';
    $xmlData .= '</AmazonEnvelope>';

    // Make the API call
    $client = new \GuzzleHttp\Client();
    $response = $client->request('POST', 'https://mws.amazonservices.com', [
        'form_params' => [
            'Action' => 'SubmitFeed',
            'SellerId' => 'YOUR_SELLER_ID',
            'MWSAuthToken' => 'YOUR_MWS_AUTH_TOKEN',
            'FeedType' => '_POST_PRODUCT_DATA_',
            'PurgeAndReplace' => 'true',
            'FeedContent' => $xmlData
        ]
    ]);

    return response()->json(['data' => $response->getBody()]);
}
Sponsor our Newsletter | Privacy Policy | Terms of Service