因为我经常使用第三方API,所以我认为创建一些Magento模块来方便地连接和查询它们会很有帮助。理想情况下,您可以像这样查询API ...
$data = Mage::getModel( 'tools/apixyz_list' )->getCollection();它将实例化其中一个列表项的模型,然后尝试通过查询API来获取这些列表项的集合。这将需要在资源模型和API之间的配置中进行某种连接,这就是我遇到的小麻烦。
有没有推荐的方法来做这件事?我很难找到关于这个主题的任何东西,但我觉得这应该是一个非常常见的问题,因为通常需要从一个项目集成到另一个项目的API数量。
发布于 2013-01-19 08:29:25
是!我实际上是为Recurly构建的-我正在尝试将其开源,但它还没有开放。下面是load()方法的一个片段,它是它的核心。
// TBT_Recurly_Model_Resource_Recurly_Abstract_Collection
public function load($printQuery = false, $logQuery = false)
{
if ($this->isLoaded()) {
return $this;
}
if ($this->_isCached()) {
return $this->_loadCache();
}
$this->_beforeLoad();
$this->_renderFilters()
->_renderOrders()
->_renderLimit();
$this->clear();
try {
# This is ultimately doing the API call
$recurly_list = $this->_getListSafe();
} catch (Recurly_Error $e) {
Mage::logException($e);
$this->setConnectionError($e->getMessage());
return $this;
}
foreach ($recurly_list as $recurly_item)
{
$item = $this->getNewEmptyItem();
$item->getResource()->setDataOnObject($item, $recurly_item);
// Recurly appears to sometimes return duplicate subscription items in it's list response.
if (!isset($this->_items[$item->getId()])) {
$this->addItem($item);
}
}
$this->_afterLoadRecurly();
// We have to setIsLoaded before we saveCache b/c otherwise it will infinite loop
$this->_setIsLoaded();
$this->_saveCache();
$this->_afterLoad();
return $this;
}实际上,我们最终将其放入了一个基本的REST类中,这真的很酷,因为在它之上实现新的REST API非常容易。
就最佳实践而言,我不确定我是否具体回答了您的问题。但基本上,我认为要让它变得干净,主要的事情是:
中实现API通信
https://stackoverflow.com/questions/14409138
复制相似问题