0

Amazon Market Place Id me retorna un xml con la lista de X productos, pero no sé como acceder digamos al atributo <ns2:Title> , la respuesta que obtengo es la siguiente:

SimpleXMLElement Object ( [ListMatchingProductsResult] => SimpleXMLElement Object ( [Products] => SimpleXMLElement Object ( [Product] => Array ( [0] => SimpleXMLElement Object ( [Identifiers] => SimpleXMLElement Object ( [MarketplaceASIN] => SimpleXMLElement Object ( [MarketplaceId] => XXXXXXXXX [ASIN] => B01N9BACDM ) ) [AttributeSets] => SimpleXMLElement Object ( ) [Relationships] => SimpleXMLElement Object ( ) [SalesRankings] => SimpleXMLElement Object ( [SalesRank] => Array ( [0] => SimpleXMLElement Object ( [ProductCategoryId] => watch_display_on_website [Rank] => 2370 ) [1] => SimpleXMLElement Object ( [ProductCategoryId] => 7012519011 [Rank] => 971 ) ) ) ) [1] => SimpleXMLElement Object ( [Identifiers] => SimpleXMLElement Object ( [MarketplaceASIN] => SimpleXMLElement Object ( [MarketplaceId] => XXXXXXXXX [ASIN] => B00ZUEMJLY ) ) [AttributeSets] => SimpleXMLElement Object ( ) [Relationships] => SimpleXMLElement Object ( ) [SalesRankings] => SimpleXMLElement Object ( [SalesRank] => Array ( [0] => SimpleXMLElement Object ( [ProductCategoryId] => watch_display_on_website [Rank] => 3392 ) [1] => SimpleXMLElement Object ( [ProductCategoryId] => 7012519011 [Rank] => 1396 ) ) ) ) [2] => SimpleXMLElement Object ( [Identifiers] => SimpleXMLElement Object ( [MarketplaceASIN] => SimpleXMLElement Object ( [MarketplaceId] => XXXXXXXXX [ASIN] => B01N7WMCMJ ) ) [AttributeSets] => SimpleXMLElement Object ( ) [Relationships] => SimpleXMLElement Object ( ) [SalesRankings] => SimpleXMLElement Object ( [SalesRank] => Array ( [0] => SimpleXMLElement Object ( [ProductCategoryId] => watch_display_on_website [Rank] => 5262 ) [1] => SimpleXMLElement Object ( [ProductCategoryId] => 7012519011 [Rank] => 2189 ) ) ) ) [3] => SimpleXMLElement Object ( [Identifiers] => SimpleXMLElement Object ( [MarketplaceASIN] => SimpleXMLElement Object ( [MarketplaceId] => XXXXXXXXX [ASIN] => B00854DIXE ) ) [AttributeSets] => SimpleXMLElement Object ( ) [Relationships] => SimpleXMLElement Object ( ) [SalesRankings] => SimpleXMLElement Object ( [SalesRank] => Array ( [0] => SimpleXMLElement Object ( [ProductCategoryId] => watch_display_on_website [Rank] => 5324 ) [1] => SimpleXMLElement Object ( [ProductCategoryId] => 7012519011 [Rank] => 2217 ) ) ) ) [4] => SimpleXMLElement Object ( [Identifiers] => SimpleXMLElement Object ( [MarketplaceASIN] => SimpleXMLElement Object ( [MarketplaceId] => XXXXXXXXX [ASIN] => B01NBE9WTU ) ) [AttributeSets] => SimpleXMLElement Object ( ) [Relationships] => SimpleXMLElement Object ( ) [SalesRankings] => SimpleXMLElement Object ( [SalesRank] => Array ( [0] => SimpleXMLElement Object ( [ProductCategoryId] => watch_display_on_website [Rank] => 2703 ) [1] => SimpleXMLElement Object ( [ProductCategoryId] => 7012519011 [Rank] => 1103 ) ) ) ) [5] => SimpleXMLElement Object ( [Identifiers] => SimpleXMLElement Object ( [MarketplaceASIN] => SimpleXMLElement Object ( [MarketplaceId] => XXXXXXXXX [ASIN] => B01H59KVCK ) ) [AttributeSets] => SimpleXMLElement Object ( ) [Relationships] => SimpleXMLElement Object ( ) [SalesRankings] => SimpleXMLElement Object ( [SalesRank] => Array ( [0] => SimpleXMLElement Object ( [ProductCategoryId] => watch_display_on_website [Rank] => 7933 ) [1] => SimpleXMLElement Object ( [ProductCategoryId] => 7012519011 [Rank] => 3399 ) ) ) ) [6] => SimpleXMLElement Object ( [Identifiers] => SimpleXMLElement Object ( [MarketplaceASIN] => SimpleXMLElement Object ( [MarketplaceId] => XXXXXXXXX [ASIN] => B01I956QMI ) ) [AttributeSets] => SimpleXMLElement Object ( ) [Relationships] => SimpleXMLElement Object ( ) [SalesRankings] => SimpleXMLElement Object ( [SalesRank] => Array ( [0] => SimpleXMLElement Object ( [ProductCategoryId] => watch_display_on_website [Rank] => 3195 ) [1] => SimpleXMLElement Object ( [ProductCategoryId] => 7012519011 [Rank] => 1311 ) ) ) ) [7] => SimpleXMLElement Object ( [Identifiers] => SimpleXMLElement Object ( [MarketplaceASIN] => SimpleXMLElement Object ( [MarketplaceId] => XXXXXXXXX [ASIN] => B005OT7MBM ) ) [AttributeSets] => SimpleXMLElement Object ( ) [Relationships] => SimpleXMLElement Object ( ) [SalesRankings] => SimpleXMLElement Object ( [SalesRank] => Array ( [0] => SimpleXMLElement Object ( [ProductCategoryId] => watch_display_on_website [Rank] => 2366 ) [1] => SimpleXMLElement Object ( [ProductCategoryId] => 7012519011 [Rank] => 967 ) ) ) ) [8] => SimpleXMLElement Object ( [Identifiers] => SimpleXMLElement Object ( [MarketplaceASIN] => SimpleXMLElement Object ( [MarketplaceId] => XXXXXXXXX [ASIN] => B00596EO4S ) ) [AttributeSets] => SimpleXMLElement Object ( ) [Relationships] => SimpleXMLElement Object ( ) [SalesRankings] => SimpleXMLElement Object ( [SalesRank] => Array ( [0] => SimpleXMLElement Object ( [ProductCategoryId] => watch_display_on_website [Rank] => 2929 ) [1] => SimpleXMLElement Object ( [ProductCategoryId] => 7012523011 [Rank] => 724 ) ) ) ) [9] => SimpleXMLElement Object ( [Identifiers] => SimpleXMLElement Object ( [MarketplaceASIN] => SimpleXMLElement Object ( [MarketplaceId] => XXXXXXXXX [ASIN] => B01N7BWYJF ) ) [AttributeSets] => SimpleXMLElement Object ( ) [Relationships] => SimpleXMLElement Object ( ) [SalesRankings] => SimpleXMLElement Object ( [SalesRank] => Array ( [0] => SimpleXMLElement Object ( [ProductCategoryId] => watch_display_on_website [Rank] => 3843 ) [1] => SimpleXMLElement Object ( [ProductCategoryId] => 7012519011 [Rank] => 1575 ) ) ) ) ) ) ) [ResponseMetadata] => SimpleXMLElement Object ( [RequestId] => 765deb6b-7ae6-4aaf-bfea-4f489a855cf0 ) )

En internet he visto formas de acceder, como por ejemplo $Objeto->atributo, pero la respuestaque yo obtengo no se parece a esos XML, ni siquiera los atributos están descritos en esa respuesta.

Cuando imprimo en xml usando (( htmlentities )) es así:

<?xml version="1.0"?> <ListMatchingProductsResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01"><ListMatchingProductsResult><Products xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01" xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd"><Product><Identifiers><MarketplaceASIN><MarketplaceId>XXXXXXXXXXXX</MarketplaceId><ASIN>B01N9BACDM</ASIN></MarketplaceASIN></Identifiers><AttributeSets><ns2:ItemAttributes xml:lang="en-CA"><ns2:BandMaterialType>Silicone</ns2:BandMaterialType><ns2:Binding>Watch</ns2:Binding><ns2:Brand>Tommy Hilfiger</ns2:Brand><ns2:ClaspType>Buckle</ns2:ClaspType><ns2:Department>mens</ns2:Department><ns2:ItemDimensions><ns2:Height Units="inches">5.91</ns2:Height><ns2:Length Units="inches">5.91</ns2:Length><ns2:Width Units="inches">5.91</ns2:Width><ns2:Weight Units="pounds">0.18</ns2:Weight></ns2:ItemDimensions><ns2:Label>Tommy Hilfiger</ns2:Label><ns2:ListPrice><ns2:Amount>235.20</ns2:Amount><ns2:CurrencyCode>CAD</ns2:CurrencyCode></ns2:ListPrice><ns2:Manufacturer>Tommy Hilfiger</ns2:Manufacturer><ns2:MaterialType>stainless-steel</ns2:MaterialType><ns2:Model>1791350</ns2:Model><ns2:PackageDimensions><ns2:Height Units="inches">3.20</ns2:Height><ns2:Length Units="inches">3.50</ns2:Length><ns2:Width Units="inches">3.30</ns2:Width><ns2:Weight Units="pounds">0.50</ns2:Weight></ns2:PackageDimensions><ns2:PackageQuantity>1</ns2:PackageQuantity><ns2:PartNumber>1791350</ns2:PartNumber><ns2:ProductGroup>Watch</ns2:ProductGroup><ns2:ProductTypeName>WATCH</ns2:ProductTypeName><ns2:Publisher>Tommy Hilfiger</ns2:Publisher><ns2:SmallImage><ns2:URL>http://ecx.images-amazon.com/images/I/51uLGBQ9ZmL._SL75_.jpg</ns2:URL><ns2:Height Units="pixels">75</ns2:Height><ns2:Width Units="pixels">53</ns2:Width></ns2:SmallImage><ns2:Studio>Tommy Hilfiger</ns2:Studio><ns2:Title>Tommy Hilfiger Men's 'COOL SPORT' Quartz Stainless Steel and Silicone Casual Watch, Color:Blue (Model: 1791350)</ns2:Title></ns2:ItemAttributes></AttributeSets><Relationships/><SalesRankings><SalesRank><ProductCategoryId>watch_display_on_website</ProductCategoryId><Rank>2370</Rank></SalesRank><SalesRank><ProductCategoryId>7012519011</ProductCategoryId><Rank>971</Rank></SalesRank></SalesRankings></Product>

El código:

$ch = curl_init($link);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt($ch , CURLOPT_RETURNTRANSFER , true);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

var_dump($response);
$xmlObject =new SimpleXMLElement($response);
print_r($xmlObject);

necesito acceder a los atributos del producto

por ejemplo si intento imprim

var_dump($xmlObject->Product);
---->
object(SimpleXMLElement)[2]

y no entiendo, porque en teoria son 10 productos

1 respuesta 1

1

Podrías intentar con FriendsOfPHP/Goutte, acabo de realizar una prueba y funciona correctamente para extraer los datos (Productos) del XML que haz compartido, solo que he tenido que cerrar algunas etiquetas del XML al final.

file.xml

<?xml version="1.0"?>
<ListMatchingProductsResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
    <ListMatchingProductsResult>
        <Products
                xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01"
                xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd">
            <Product>
                <Identifiers>
                    <MarketplaceASIN>
                        <MarketplaceId>XXXXXXXXXXXX</MarketplaceId>
                        <ASIN>B01N9BACDM</ASIN>
                    </MarketplaceASIN>
                </Identifiers>
                <AttributeSets>
                    <ns2:ItemAttributes xml:lang="en-CA">
                        <ns2:BandMaterialType>Silicone</ns2:BandMaterialType>
                        <ns2:Binding>Watch</ns2:Binding>
                        <ns2:Brand>Tommy Hilfiger</ns2:Brand>
                        <ns2:ClaspType>Buckle</ns2:ClaspType>
                        <ns2:Department>mens</ns2:Department>
                        <ns2:ItemDimensions>
                            <ns2:Height Units="inches">5.91</ns2:Height>
                            <ns2:Length Units="inches">5.91</ns2:Length>
                            <ns2:Width Units="inches">5.91</ns2:Width>
                            <ns2:Weight Units="pounds">0.18</ns2:Weight>
                        </ns2:ItemDimensions>
                        <ns2:Label>Tommy Hilfiger</ns2:Label>
                        <ns2:ListPrice>
                            <ns2:Amount>235.20</ns2:Amount>
                            <ns2:CurrencyCode>CAD</ns2:CurrencyCode>
                        </ns2:ListPrice>
                        <ns2:Manufacturer>Tommy Hilfiger</ns2:Manufacturer>
                        <ns2:MaterialType>stainless-steel</ns2:MaterialType>
                        <ns2:Model>1791350</ns2:Model>
                        <ns2:PackageDimensions>
                            <ns2:Height Units="inches">3.20</ns2:Height>
                            <ns2:Length Units="inches">3.50</ns2:Length>
                            <ns2:Width Units="inches">3.30</ns2:Width>
                            <ns2:Weight Units="pounds">0.50</ns2:Weight>
                        </ns2:PackageDimensions>
                        <ns2:PackageQuantity>1</ns2:PackageQuantity>
                        <ns2:PartNumber>1791350</ns2:PartNumber>
                        <ns2:ProductGroup>Watch</ns2:ProductGroup>
                        <ns2:ProductTypeName>WATCH</ns2:ProductTypeName>
                        <ns2:Publisher>Tommy Hilfiger</ns2:Publisher>
                        <ns2:SmallImage>
                            <ns2:URL>http://ecx.images-amazon.com/images/I/51uLGBQ9ZmL._SL75_.jpg</ns2:URL>
                            <ns2:Height Units="pixels">75</ns2:Height>
                            <ns2:Width Units="pixels">53</ns2:Width>
                        </ns2:SmallImage>
                        <ns2:Studio>Tommy Hilfiger</ns2:Studio>
                        <ns2:Title>Tommy Hilfiger Men's 'COOL SPORT' Quartz Stainless Steel and Silicone Casual Watch, Color:Blue (Model: 1791350)</ns2:Title>
                    </ns2:ItemAttributes>
                </AttributeSets>
                <Relationships/>
                <SalesRankings>
                    <SalesRank>
                        <ProductCategoryId>watch_display_on_website</ProductCategoryId>
                        <Rank>2370</Rank>
                    </SalesRank>
                    <SalesRank>
                        <ProductCategoryId>7012519011</ProductCategoryId>
                        <Rank>971</Rank>
                    </SalesRank>
                </SalesRankings>
            </Product>
        </Products>
    </ListMatchingProductsResult>
</ListMatchingProductsResponse>

index.php

<?php

include('vendor/autoload.php');

use Goutte\Client;

$client = new Client();
$crawler = $client->request('GET', 'http://localhost:8000/file.xml');

$products = $crawler->filter('default|Products default|Product')->each(function ($node) {

    /** @var $node \Symfony\Component\DomCrawler\Crawler */
    $marketplaceId = $node->filter('default|Identifiers default|MarketplaceASIN default|MarketplaceId')->text();
    $attributeBrand = $node->filter('default|AttributeSets ns2|ItemAttributes ns2|Brand')->text();

    return [
        'MarketplaceId' => $marketplaceId,
        'attributeBrand' => $attributeBrand
    ];

});

echo '<pre>';
print_r($products);
echo '</pre>';

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.