使用Magento的API,以获取产品 [英] Using Magento API to get Products

查看:242
本文介绍了使用Magento的API,以获取产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Magento的API来从另一个域特定类别获取产品的产品数据。我所做的API调用等...我目前使用来获取产品数据看起来像这样的code:

I'm using the Magento API to get product data for products from a certain category from another domain. I have made the API call etc... The code I'm currently using to get the product data looks like this:

$productList = $client->call($session, 'catalog_category.assignedProducts', 7);

foreach ($productList as $product){
    $theProduct = array();
    $theProduct['info'] = $client->call($session, 'catalog_product.info', $product['sku']);
    $allProducts[] = $theProduct;
}

在code正常工作,而且走得非常慢。当我添加图像调用循环需要大约50秒的页面加载,而这对于只有5个产品的网站。我想知道的是以下内容:

The code works fine, but it goes extremely slow. When I add the image call to the loop it takes about 50 seconds for the page to load, and that's for a site with only 5 products. What I want to know is the following:


  1. 是code以上正确的,它只是Magento的API剧本很慢?

  2. 是code上面没有做的最好的方法是什么,我需要?

  3. 难道还有其他因素使这一走这么慢?

任何帮助将是非常美联社preciated。至少如果我知道我使用我可以看看其他的途径code正确。

Any help would be much appreciated. At least if I know I'm using the code right I can look at other avenues.

在此先感谢!

=================编辑=================

使用多重呼叫由马蒂亚斯ZEIS建议,数据到达快得多。这里的code我用:

Using multicall suggested by Matthias Zeis, the data arrives much quicker. Here's the code I used:

$apicalls = array();
$i = 0;
$productList = $client->call($session, 'catalog_category.assignedProducts', 7);

foreach ($productList as $product){
$apicalls[$i] = array('catalog_product.info', $product['product_id']);
$i++;
}

$list = $client->multiCall($session, $apicalls);

这现在工作比以前快多了!下一个问题我发现的是,<一个href=\"http://www.magentocommerce.com/wiki/doc/webservices-api/api/catalog_product_attribute_media#catalog_product_attribute_media.list\">catalog_product_attribute_media.list电话似乎没有以同样的方式工作,尽管这些产品都纷纷图像设定。

This now works much quicker than before! The next issue I've found is that the catalog_product_attribute_media.list call doesn't seem to work in the same way, even though the products all have images set.

我得到在后续代码var_dump的错误是:

The error I'm getting in the var_dump is:

请求图像不存在于产品形象的画廊。

Requested image not exists in product images' gallery.

有人知道为什么这可能现在是这样吗?再次感谢在前进。

Anybody know why this may now be happening? Thanks again in advance.

推荐答案

1。是code以上正确的,它只是Magento的API剧本很慢?

您code是正确的,但剧本是缓慢的,因为(一)SOAP API是不是很快和(b)要为每一件单品做单独的电话。

Your code is correct, but the script is slow because (a) the SOAP API is not blazingly fast and (b) you are doing seperate calls for every single product.

2。是code上面没有做的最好的方法是什么,我需要?

如果您使用SOAP 1.0版API或XML-RPC,你可以的 多重呼叫 。起初,呼吁catalog_category.assignedProducts来获取产品ID。收集的产品ID和执行多重呼叫电话。这应该减少等候时间下降了不少。

If you use the SOAP v1 API or XML-RPC, you can test multiCall. At first, call catalog_category.assignedProducts to fetch the product ids. Collect the product ids and execute a multiCall call. That should cut the waiting time down quite a bit.

不幸的是,Magento的不提供很好的解决方案开箱像你需要它来传递数据。我建议你​​实现自定义的API调用

Unfortunately, Magento doesn't provide a nice solution out of the box to deliver the data like you need it. I recommend that you implement your own custom API call.

使用一个产品集合模型:

Use a product collection model:

$collection = Mage::getModel('catalog/product')->getCollection();

这将让你可以用来进行筛选,排序,分页,......你的产品列表中Mage_Catalog_Model_Resource_Product_Collection对象。遍历集合,并构建包含您所需要的数据的数组。您也可以直接生成缩略图为您的产品,同时建立数据数组:

This will get you a Mage_Catalog_Model_Resource_Product_Collection object which can be used to filter, sort, paginate, ... your product list. Iterate over the collection and build an array containing the data you need. You also can generate thumbnails for your products directly while building the data array:

foreach ($products as $product) {
    $data[$product->getSku()] = array(
        /* the attributes no need ... */
        'small_image'   => Mage::helper('catalog/image')->init($product, 'image')
                                ->constrainOnly(true)
                                ->keepAspectRatio(true)
                                ->keepFrame(false)
                                ->resize(100,150)
                                ->__toString(),
        /* some more attributes ... */
    );
}

这应该给你相当的性能提升。

This should give you quite a performance improvement.

不过,当然这仅仅是冰山的提示。如果此解决方案是不够快你,避免SOAP并通过建立的自己的API绕过Magento的堆栈的一部分。这并不一定是一个复杂的解决方案:它可能是一个简单的PHP脚本使用HTTP基本身份验证,它解析为过滤器条件等网址,包括应用程序/ Mage.php,并呼吁法师::应用程序()初始化Magento的框架。这样做的好处是,你必须使用的Magento类的舒适性,而且你不必去通过整个路由过程。

But of course this only is the tip of the iceberg. If this solution is not fast enough for you, avoid SOAP and bypass a part of the Magento stack by building your own API. This doesn't have to be a complex solution: it could be a simple PHP script with HTTP Basic Authentication which parses the URL for filter criteria etc., includes app/Mage.php and calls Mage::app() to initialise the Magento framework. The benefit is that you have the comfort of using Magento classes but you don't have to go through the whole routing process.

不要忘了,你可能会缓存中的结果,因为我能想象,你会表现出同样的产品,在其他领域也不少游客。即使缓存几分钟可以帮助您的服务器。

Not to forget, you may cache the results because I could imagine that you will show the same products to quite a few visitors on the other domain. Even caching for a few minutes may help your server.

3。难道还有其他因素使这一走这么慢?

有可能会出现一些原因导致呼叫的的速度慢的服务器上 - 但不知道你的数据量,你的服务器硬件,并且已经完成了客​​户化,即使是最好的猜测赢得 T为好。

There may be some reasons why the calls are that slow on your server - but without knowing the volume of your data, your server hardware and the customisations you have done, even a best guess won't be that good.

这篇关于使用Magento的API,以获取产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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