格式化来自cUrl请求的响应

z9smfwbn  于 2023-06-30  发布在  其他
关注(0)|答案(1)|浏览(121)

我正在亚马逊MWS上工作,并试图使用GetMatchingProductForId从SKU中获取产品详细信息。我得到的回应是:

SimpleXMLElement Object
(
    [GetMatchingProductForIdResult] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [Id] => VYPL039_$P
                    [IdType] => SellerSKU
                    [status] => Success
                )

            [Products] => SimpleXMLElement Object
                (
                    [Product] => SimpleXMLElement Object
                        (
                            [Identifiers] => SimpleXMLElement Object
                                (
                                    [MarketplaceASIN] => SimpleXMLElement Object
                                        (
                                            [MarketplaceId] => A21TJRUUN4KGV
                                            [ASIN] => B00NA3ZMKM
                                        )

                                )

                            [AttributeSets] => SimpleXMLElement Object
                                (
                                )

                            [Relationships] => SimpleXMLElement Object
                                (
                                )

                            [SalesRankings] => SimpleXMLElement Object
                                (
                                )

                        )

                )

        )

    [ResponseMetadata] => SimpleXMLElement Object
        (
            [RequestId] => 9e893248-adaf-43a1-bcae-70f62b6888c7
        )

)

cURL请求:

$url = "https://mws.amazonservices.in/Products/2011-10-01" . '?' . $url_string . "&Signature=" . $signature;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        $response = curl_exec($ch); 

        //echo $url;exit;

$responseDoc = new DOMDocument();
        $responseDoc->loadXML($response);

        $response = simplexml_import_dom($responseDoc);

echo '<pre>';
        print_r($response);
        echo '</pre>';
        exit;

但是为什么响应较少,但是当我在浏览器中运行URL时,它给了我如下的完整响应:-

<GetMatchingProductForIdResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
<GetMatchingProductForIdResult Id="VYPL039_$P" IdType="SellerSKU" status="Success">
<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>A21TJRUUN4KGV</MarketplaceId>
<ASIN>B00NA3ZMKM</ASIN>
</MarketplaceASIN>
</Identifiers>
<AttributeSets>
<ns2:ItemAttributes xml:lang="en-IN">
<ns2:Binding>Apparel</ns2:Binding>
<ns2:Brand>Vivekananda Youth Connect</ns2:Brand>
<ns2:Department>womens</ns2:Department>
<ns2:Feature>Category :Clothing</ns2:Feature>
<ns2:Feature>Sleeves : Half</ns2:Feature>
<ns2:Feature>Trends : Printed</ns2:Feature>
<ns2:Label>Vivekananda Youth Connect</ns2:Label>
<ns2:Manufacturer>Vivekananda Youth Connect</ns2:Manufacturer>
<ns2:MaterialType>Cotton</ns2:MaterialType>
<ns2:PackageDimensions>
<ns2:Height Units="inches">2.00</ns2:Height>
<ns2:Length Units="inches">15.00</ns2:Length>
<ns2:Width Units="inches">12.00</ns2:Width>
<ns2:Weight Units="pounds">0.77</ns2:Weight>
</ns2:PackageDimensions>
<ns2:PartNumber>VYPL039_$P</ns2:PartNumber>
<ns2:ProductGroup>Apparel</ns2:ProductGroup>
<ns2:ProductTypeName>SHIRT</ns2:ProductTypeName>
<ns2:Publisher>Vivekananda Youth Connect</ns2:Publisher>
<ns2:SmallImage>
<ns2:URL>
http://g-ecx.images-amazon.com/images/G/31/x-site/icons/no-img-sm._V138359930_.gif
</ns2:URL>
<ns2:Height Units="pixels">40</ns2:Height>
<ns2:Width Units="pixels">60</ns2:Width>
</ns2:SmallImage>
<ns2:Studio>Vivekananda Youth Connect</ns2:Studio>
<ns2:Title>
Vivekananda Youth Connect Happiness Mantra Womens Tshirt_VYPL039_$P
</ns2:Title>
</ns2:ItemAttributes>
</AttributeSets>
<Relationships>
<ns2:VariationChild>
<Identifiers>
<MarketplaceASIN>
<MarketplaceId>A21TJRUUN4KGV</MarketplaceId>
<ASIN>B00NA4AFN0</ASIN>
</MarketplaceASIN>
</Identifiers>
<ns2:Color>Orange</ns2:Color>
<ns2:Size>36</ns2:Size>
</ns2:VariationChild>
<ns2:VariationChild>
<Identifiers>
<MarketplaceASIN>
<MarketplaceId>A21TJRUUN4KGV</MarketplaceId>
<ASIN>B00NA4AKPI</ASIN>
</MarketplaceASIN>
</Identifiers>
<ns2:Color>Orange</ns2:Color>
<ns2:Size>38</ns2:Size>
</ns2:VariationChild>
<ns2:VariationChild>
<Identifiers>
<MarketplaceASIN>
<MarketplaceId>A21TJRUUN4KGV</MarketplaceId>
<ASIN>B00NA4AOWC</ASIN>
</MarketplaceASIN>
</Identifiers>
<ns2:Color>Orange</ns2:Color>
<ns2:Size>40</ns2:Size>
</ns2:VariationChild>
</Relationships>
<SalesRankings/>
</Product>
</Products>
</GetMatchingProductForIdResult>
<ResponseMetadata>
<RequestId>161416fd-f047-4b13-93d5-27df3f428c5a</RequestId>
</ResponseMetadata>
</GetMatchingProductForIdResponse>

编辑:-

我现在已经将响应格式更改为SimpleXMLElement::asXML:

$url = "https://mws.amazonservices.in/Products/2011-10-01" . '?' . $url_string . "&Signature=" . $signature;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        $response = curl_exec($ch);

$parsed_xml = simplexml_load_string($response); 
$response = $parsed_xml->asXML();

但是$response什么也不返回。

x6492ojm

x6492ojm1#

我不知道这对你来说是不是合适的解决方案,但它对我来说很好:

$response = str_replace("ns2:","",$response);
$responseDoc = new DOMDocument();
$responseDoc->loadXML($response);

$response = simplexml_import_dom($responseDoc);

echo '<pre>';
print_r($response);
echo '</pre>';
exit;

请检查上面的代码。

相关问题