2016-02-11 101 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可以不會被解碼。 –