您好我想使用从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();
其中一个产品的Amazon响应示例:
[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
)
)
)
)
)
答案 0 :(得分:1)
我不确定亚马逊API的具体细节,所以我要做的第一件事就是研究亚马逊关于如何获取字符串描述的文档。
如果没有,查看该结果,描述是结构化数据。例如,在这种情况下,它是一个轨道列表和一个ID。如果您需要从中获取描述,可以先使用以下命令将stdClass转换为数组:
json_decode(json_encode($item), true);
然后,一旦这是一个数组,你可以递归地遍历它并编译一个字符串。如果它是一维数组,你可以简单地使用带分隔符的implode将它连接在一起,但是在这种情况下它是一个多维数组。
但是,我应该重新迭代,这应该是最后度假胜地。尽可能努力地找到首先在亚马逊上显示描述的最佳实践。