2016-02-11 45 views
2

您好,我想使用从Amazon api收集的数据设置magento产品的描述。我调用API,并在日志中我得到不过接收响应:如何从Amazon API解析stdClass响应返回字符串

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

的问题是如何解析的信息转换成字符串,因此可以在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 
          ) 
        ) 
      ) 
    ) 
) 

回答

1

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

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

json_decode(json_encode($item), true); 

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

但是,我应该再次重申,这应该是LAST度假村。首先尽可能努力地找到显示亚马逊描述的最佳实践。

+0

不幸的是,当我将它添加到我的代码中时,它不显示任何内容。但是,谢谢你的回答。 –

+0

该代码不显示任何内容,它只返回一些内容。如果你想让它显示出来,你必须回显它。 你可以用'$ item = json_decode(json_encode($ item),true))' –

+0

将它指定给一个变量我的不好,因为它没有显示任何我表示“NULL”,这意味着JSON可以不会被解码。 –