Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you parse Relationships in MWS GetMatchingProduct?

The Data:

<Relationships>
      <ns2:VariationChild>
        <Identifiers>
          <MarketplaceASIN>
            <MarketplaceId>ATVPDKIKX0DER</MarketplaceId>
            <ASIN>B002KT3XQC</ASIN>
          </MarketplaceASIN>
        </Identifiers>
        <ns2:Color>Black</ns2:Color>
        <ns2:Size>Small</ns2:Size>
      </ns2:VariationChild>
      <ns2:VariationChild>
        <Identifiers>
          <MarketplaceASIN>
            <MarketplaceId>ATVPDKIKX0DER</MarketplaceId>
            <ASIN>B002KT3XQW</ASIN>
          </MarketplaceASIN>
        </Identifiers>
        <ns2:Color>Black</ns2:Color>
        <ns2:Size>Medium</ns2:Size>
      </ns2:VariationChild>
</Relationships>

The Code:

$data = simplexml_load_string($response);
foreach($data->GetMatchingProductResult AS $GetMatchingProductResult){
     $Product = $GetMatchingProductResult->Product;
     $Relationships = $Product->Relationships;

     foreach($Relationships->children('ns2', true)->VariationChild AS $VariationChild){

          $Identifiers = $VariationChild->Identifiers;
               $MarketplaceASIN = $Identifiers->MarketplaceASIN;
                    $MarketplaceId = $MarketplaceASIN->MarketplaceId;
                    $ASIN = $MarketplaceASIN->ASIN;

                    echo "$ASIN<br />";

     }
}

This echos the returns, but no data so it is actually looping through the XML. However nothing I try will actually return data in the $ASIN variable. Is this because of the namespace, or simpleXML, or am I missing something else entirely?

edit: other methods tried

foreach($Relationships->children('http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd', true)->VariationChild AS $VariationChild){

     $Identifiers           = $VariationChild->Identifiers;
          $MarketplaceASIN  = $Identifiers->MarketplaceASIN;
               $MarketplaceId   = $MarketplaceASIN->MarketplaceId;
               $ASIN            = $MarketplaceASIN->ASIN;

               echo "[$ASIN]<br />";

}

$test = new SimpleXMLElement($response);
$test->registerXPathNamespace('ns2', 'http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd');
$variations = $test->xpath('//ns2:VariationChild');

foreach($variations AS $vars){

     print_r($vars);

}

Neither seems to even loop the data.

like image 944
fwho Avatar asked Sep 20 '16 00:09

fwho


2 Answers

The following code fetches the ASIN strings:

$data = simplexml_load_string($response);

foreach ($data->GetMatchingProductResult as $GetMatchingProductResult) {
  $Product = $GetMatchingProductResult->Product;
  $Relationships = $Product->Relationships;

  foreach ($Relationships->children('ns2', true)->VariationChild
    as $VariationChild)
  {
    foreach ($VariationChild->children('', true) as $var_child) {
      echo $var_child->MarketplaceASIN->ASIN, PHP_EOL;
    }
  }
}

It's worth mentioning, the real response format differs from what you have posted.

like image 131
Ruslan Osmanov Avatar answered Nov 16 '22 02:11

Ruslan Osmanov


Yes, It's the namespace used incorrectly. Replace 'ns2' in your code with the full namespace "http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd".

A better solution imo is to use registerXPathNamespace and then use xpath to access the child elements.

like image 20
jad Avatar answered Nov 16 '22 03:11

jad