How do you parse Relationships in MWS GetMatchingProduct?
Asked Answered
Y

2

9

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.

Yvette answered 20/9, 2016 at 0:29 Comment(0)
P
6

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.

Parasitic answered 5/10, 2016 at 13:51 Comment(0)
P
2

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.

Portage answered 24/9, 2016 at 10:41 Comment(1)
Thanks! Unfortunately, neither method worked for me. I posted an edit above, but it seems like it is not even looping the data.Yvette

© 2022 - 2024 — McMap. All rights reserved.