如何从亚马逊的API响应解析stdClass的返回字符串 [英] How to parse stdClass from Amazon API Response to return string

查看:373
本文介绍了如何从亚马逊的API响应解析stdClass的返回字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我想设置为使用来自Amazon的API收集的数据Magento的产品说明我调用API,并在日志中我得到不过接收响应:


  

可恢复错误:类stdClass的客体不能转换为字符串


问题是如何解析的信息转换成字符串,因此它可以在Magento的产品的详细信息可以使用?

 < PHP
require_once'../abstract.php';
需要('AmazonApi.php');类Mage_Shell_Amazon扩展Mage_Shell_Abstract
{
公共职能的run(){    //创建API访问对象
    $ PUBLIC_KEY ='*********';
    $ SECRET_KEY ='********* + *******;
    $ associate_tag ='******* - 21';
    $ amazon_api =新AmazonAPI($ PUBLIC_KEY,$ SECRET_KEY,$ associate_tag);
    //负荷由产品的categoryId
    $产品=法师:: getModel('目录/产品)
         - > getCollection()
         - > addAttributeToSelect('阿辛')
         - > addAttributeToSelect(说明);    //请求参数数组
    的foreach($产品为$ PROD)
    {
        //加载实际的产品数据
        $产品=法师:: getModel('目录/产品') - GT;负载($的精良>的getId());        $ ASIN = $产品 - > getAsin();        $ params_array =阵列(
            操作= GT; ItemLookup',
            IdType'=> ASIN,
            ITEMID'=> $ ASIN,
            ResponseGroup'=> 轨道);        //返回项搜索查询慢魔术师'名单
        $响应= $ amazon_api-> sendRequest将($ params_array);        $产品展示> setDescription($ restponse);
        $产品展示>的getResource() - GT; saveAttribute($产品,说明);        的foreach($响应为$ restponse)
        {
            睡眠(1);
        }
        呼应'< pre>';
        的print_r($ restponse);
        呼应'< / pre>';    }    //的foreach($ parsed_xml-> OperationRequest->错误 - >错误为$错误){
    //回声错误code:。 $无差错> code。 \\ r \\ n;
    //回声$错误 - >消息。 \\ r \\ n;
    //回声\\ r \\ n;
     //}
       }
     }        $ amazonConnector =新Mage_Shell_Amazon();
        $ amazonConnector->运行();

从产品之一亚马逊响应样本:

  [项目] => stdClass的对象
    (
        [要求] => stdClass的对象
            (
                [的IsValid] =>真正
                [ItemLooku prequest] => stdClass的对象
                    (
                        [IdType] => ASIN
                        [ITEMID] => B000002OGL
                        [ResponseGroup] =>曲目
                        [VariationPage] =>所有
                    )            )        [项目] => stdClass的对象
            (
                [ASIN] => B000002OGL
                [曲目] => stdClass的对象
                    (
                        [光盘] ​​=> stdClass的对象
                            (
                                [田径] =>排列
                                    (
                                        [0] => stdClass的对象
                                            (
                                                [_] =>野马莎莉
                                                [编号] => 1
                                            )                                        [1] => stdClass的对象
                                            (
                                                [_] =>带我去河
                                                [编号] => 2
                                            )                                        [2] => stdClass的对象
                                            (
                                                [_] =>链怪事
                                                [编号] => 3
                                            )                                        [3] => stdClass的对象
                                            (
                                                [_] =>暗结的街
                                                [编号] => 4
                                            )                                        [4] => stdClass的对象
                                            (
                                                [_] =>目标:任何
                                                [编号] =>五
                                            )                                        [5] => stdClass的对象
                                            (
                                                [_] =>我不能忍受雨
                                                [编号] => 6
                                            )                                        [6] => stdClass的对象
                                            (
                                                [_] =>尝试一点温柔
                                                [编号] => 7
                                            )                                        [7] => stdClass的对象
                                            (
                                                [_] =>对待我的权利
                                                [编号] => 8
                                            )                                        [8] => stdClass的对象
                                            (
                                                [_] =>做对女人做对的人
                                                [编号] => 9
                                            )                                        [9] => stdClass的对象
                                            (
                                                [_] =>可怜的先生
                                                [编号] => 10
                                            )                                        [10] => stdClass的对象
                                            (
                                                [_] =>我从来没有爱过一个人
                                                [编号] => 11
                                            )                                        [11] => stdClass的对象
                                            (
                                                [_] =>在午夜时分
                                                [编号] => 12
                                            )                                        [12] => stdClass的对象
                                            (
                                                [_] =>再见宝贝
                                                [编号] => 13
                                            )                                        [13] => stdClass的对象
                                            (
                                                [_] =>溜
                                                [编号] => 14
                                            )                                    )                                [编号] => 1
                            )
                    )
            )
    )
  )


解决方案

我不知道在与亚马逊API细节,所以我会做的第一件事情会在亚马逊如何获得一个字符串描述文件研究。

如果不是,在看该结果,上述说明的结构化数据。例如,在这种情况下,它的轨道和一个ID的列表。如果你需要得到一个说明从您可以先使用转换stdClass已经到一个数组:

  json_de code(json_en code($项目),TRUE);

,然后一旦这是一个数组,你可以通过它走递归和编译字符串。如果它是一个一维阵列,可以简单地使用内爆用定界符加入在一起,但是在这种情况下,它是一个多维阵列

但同样,我应该重新迭代,这应该是过去度假胜地。尝试努力,你可以找到第一个从亚马逊显示描述的最佳实践。

Hello I would like to set the Description for magento products using data collected from Amazon api. I am calling the API and receiving the response however in the logs I get :

Recoverable Error: Object of class stdClass could not be converted to string

The question is how to parse the information into a string so it can be used within magento product details?

<?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();

Sample from Amazon response for one of the products :

[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
                            )
                    )
            )
    )
  )

解决方案

I'm not sure on specifics with the Amazon API, so the first thing I would do would research on Amazon's documentation on how to get a string description.

If not, looking at that result, the description is structured data. For example, in this case it's a list of tracks and an ID. If you NEED to get a description from that you can first convert the stdClass into an array using:

json_decode(json_encode($item), true);

And then once that's an array, you could walk through it recursively and compile a string. If it was a one-dimensional array, you could simply use implode with a delimiter to join it together, however in this case it is a multi-dimensional array.

But again, I should re-iterate, this should be the LAST resort. Try as hard as you can to find the best practices for displaying descriptions from Amazon first.

这篇关于如何从亚马逊的API响应解析stdClass的返回字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆