【问题标题】:How to parse stdClass from Amazon API Response to return string如何从 Amazon API 响应中解析 stdClass 以返回字符串
【发布时间】:2016-05-22 05:29:41
【问题描述】:

您好,我想使用从 Amazon api 收集的数据设置 magento 产品的描述。我正在调用 API 并接收响应,但是在我得到的日志中:

可恢复的错误:stdClass 类的对象无法转换为字符串

问题是如何将信息解析成字符串,以便在magento产品详细信息中使用?

<?php
require_once '../abstract.php';
require('AmazonApi.php');

class Mage_Shell_Amazon extends Mage_Shell_Abstract
{


public function run() {

    //Create API access object
    $public_key = '*********';
    $secret_key = '*********+*******';
    $associate_tag = '*******-21';
    $amazon_api = new AmazonAPI($public_key, $secret_key, $associate_tag);


    //load product by categoryId
    $products = Mage::getModel('catalog/product')
        ->getCollection()
        ->addAttributeToSelect('asin')
        ->addAttributeToSelect('description');

    //Array of request parameters
    foreach($products as $prod)
    {
        //load the actual products data
        $product = Mage::getModel('catalog/product')->load($prod->getId());

        $asin = $product->getAsin();

        $params_array = array(
            'Operation' => 'ItemLookup',
            'IdType' => 'ASIN',
            'ItemId' => $asin ,
            'ResponseGroup' => 'Tracks');

        // returns a list of items for the search query 'Slow Magic'
        $response = $amazon_api->sendRequest($params_array);

        $product->setDescription($restponse);
        $product->getResource()->saveAttribute($product, 'description');

        foreach ($response as $restponse)
        {
            sleep(1);
        }
        echo '<pre>';
        print_r($restponse);
        echo '</pre>';

    }

    //        foreach($parsed_xml->OperationRequest->Errors->Error as $error){
    //            echo "Error code: " . $error->Code . "\r\n";
    //            echo $error->Message . "\r\n";
    //            echo "\r\n";
     //        }
       }
     }

        $amazonConnector = new Mage_Shell_Amazon();
        $amazonConnector->run();

来自亚马逊对其中一种产品的响应示例:

[Items] => stdClass Object
    (
        [Request] => stdClass Object
            (
                [IsValid] => True
                [ItemLookupRequest] => stdClass Object
                    (
                        [IdType] => ASIN
                        [ItemId] => B000002OGL
                        [ResponseGroup] => Tracks
                        [VariationPage] => All
                    )

            )

        [Item] => stdClass Object
            (
                [ASIN] => B000002OGL
                [Tracks] => stdClass Object
                    (
                        [Disc] => stdClass Object
                            (
                                [Track] => Array
                                    (
                                        [0] => stdClass Object
                                            (
                                                [_] => Mustang Sally
                                                [Number] => 1
                                            )

                                        [1] => stdClass Object
                                            (
                                                [_] => Take Me To The River
                                                [Number] => 2
                                            )

                                        [2] => stdClass Object
                                            (
                                                [_] => Chain Of Fools
                                                [Number] => 3
                                            )

                                        [3] => stdClass Object
                                            (
                                                [_] => The Dark End Of The Street
                                                [Number] => 4
                                            )

                                        [4] => stdClass Object
                                            (
                                                [_] => Destination: Anywhere
                                                [Number] => 5
                                            )

                                        [5] => stdClass Object
                                            (
                                                [_] => I Can't Stand The Rain
                                                [Number] => 6
                                            )

                                        [6] => stdClass Object
                                            (
                                                [_] => Try A Little Tenderness
                                                [Number] => 7
                                            )

                                        [7] => stdClass Object
                                            (
                                                [_] => Treat Me Right
                                                [Number] => 8
                                            )

                                        [8] => stdClass Object
                                            (
                                                [_] => Do Right Woman Do Right Man
                                                [Number] => 9
                                            )

                                        [9] => stdClass Object
                                            (
                                                [_] => Mr. Pitiful
                                                [Number] => 10
                                            )

                                        [10] => stdClass Object
                                            (
                                                [_] => I Never Loved A Man
                                                [Number] => 11
                                            )

                                        [11] => stdClass Object
                                            (
                                                [_] => In The Midnight Hour
                                                [Number] => 12
                                            )

                                        [12] => stdClass Object
                                            (
                                                [_] => Bye Bye Baby
                                                [Number] => 13
                                            )

                                        [13] => stdClass Object
                                            (
                                                [_] => Slip Away
                                                [Number] => 14
                                            )

                                    )

                                [Number] => 1
                            )
                    )
            )
    )
  )

【问题讨论】:

    标签: php xml api amazon-web-services


    【解决方案1】:

    我不确定 Amazon API 的具体细节,所以我要做的第一件事就是研究 Amazon 的文档,了解如何获取字符串描述。

    如果不是,则查看该结果,说明是结构化数据。例如,在这种情况下,它是曲目列表和 ID。如果您需要从中获取描述,您可以首先使用以下方法将 stdClass 转换为数组:

    json_decode(json_encode($item), true);
    

    然后,一旦这是一个数组,您就可以递归地遍历它并编译一个字符串。如果它是一个一维数组,你可以简单地使用带有分隔符的 implode 将它连接在一起,但是在这种情况下它是一个多维数组。

    但是,我应该再次重申,这应该是最后的手段。尽你所能找到首先显示来自亚马逊的描述的最佳实践。

    【讨论】:

    • 不幸的是,当我将它添加到我的代码中时,它没有显示任何内容。不过谢谢你的回答。
    • 该代码不显示任何内容,它只返回一些内容。如果你想让它显示你必须回显它。您可以使用 $item = json_decode(json_encode($item), true)) 将其分配给变量
    • 我的错,因为它没有显示任何我的意思是“NULL”,这意味着 JSON 无法被解码。
    猜你喜欢
    • 2012-12-27
    • 2020-07-12
    • 2017-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-14
    • 2021-06-03
    • 2023-03-27
    相关资源
    最近更新 更多